MCPcopy Create free account
hub / github.com/dask/dask / __setitem__

Method __setitem__

dask/array/core.py:1903–1977  ·  view source on GitHub ↗
(self, key, value)

Source from the content-addressed store, hash-verified

1901 return self._scalarfunc(operator.index)
1902
1903 def __setitem__(self, key, value):
1904 if value is np.ma.masked:
1905 value = np.ma.masked_all((), dtype=self.dtype)
1906
1907 if not is_dask_collection(value) and self.dtype.kind in "iu":
1908 if np.isnan(value).any():
1909 raise ValueError("cannot convert float NaN to integer")
1910 if np.isinf(value).any():
1911 raise ValueError("cannot convert float infinity to integer")
1912
1913 # Suppress dtype broadcasting; __setitem__ can't change the dtype.
1914 # Use asanyarray to retain e.g. np.ma objects.
1915 value = asanyarray(value, dtype=self.dtype, like=self)
1916
1917 if isinstance(key, Array) and (
1918 key.dtype.kind in "iu"
1919 or (key.dtype == bool and key.ndim == 1 and self.ndim > 1)
1920 ):
1921 key = (key,)
1922
1923 ## Use the "where" method for cases when key is an Array of bools
1924 if isinstance(key, Array):
1925 from dask.array.routines import where
1926
1927 left_shape = np.array(key.shape)
1928 right_shape = np.array(self.shape)
1929
1930 # We want to treat unknown shape on *either* sides as a match
1931 match = left_shape == right_shape
1932 match |= np.isnan(left_shape) | np.isnan(right_shape)
1933
1934 if not match.all():
1935 raise IndexError(
1936 f"boolean index shape {key.shape} must match indexed array's "
1937 f"{self.shape}."
1938 )
1939
1940 # If value has ndim > 0, they must be broadcastable to self.shape[idx].
1941 # This raises when the bool mask causes the size to become unknown,
1942 # e.g. this is valid in numpy but raises here:
1943 # x = da.array([1,2,3])
1944 # x[da.array([True, True, False])] = [4, 5]
1945 if value.ndim:
1946 value = broadcast_to(value, self[key].shape)
1947
1948 y = where(key, value, self)
1949 # FIXME does any backend allow mixed ops vs. numpy?
1950 # If yes, is it wise to let them change the meta?
1951 self._meta = y._meta
1952 self.dask = y.dask
1953 self._name = y.name
1954 self._chunks = y.chunks
1955 return
1956
1957 if np.isnan(self.shape).any():
1958 raise ValueError(f"Arrays chunk sizes are unknown. {unknown_chunk_message}")
1959
1960 # Still here? Then apply the assignment to other type of

Callers

nothing calls this directly

Calls 11

is_dask_collectionFunction · 0.90
whereFunction · 0.90
setitem_arrayFunction · 0.90
meta_from_arrayFunction · 0.90
broadcast_toFunction · 0.85
from_collectionsMethod · 0.80
asanyarrayFunction · 0.70
ArrayClass · 0.70
tokenizeFunction · 0.50
anyMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected