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

Method __setitem__

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

Source from the content-addressed store, hash-verified

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