MCPcopy
hub / github.com/dask/dask / __setitem__

Method __setitem__

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

Source from the content-addressed store, hash-verified

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