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

Method __setitem__

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

Source from the content-addressed store, hash-verified

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