docs/source/array-assignment.rst
.. _array.assignment:
Dask Array supports most of the NumPy assignment indexing syntax. In particular, it supports combinations of the following:
x[1] = yx[2::-1] = yx[[0, -1, 1]] = ynumpy array of integers: x[np.arange(3)] = y~dask.array.Array of integers: x[da.arange(3)] = y, x[da.from_array([0, -1, 1])] = y, x[da.where(np.array([1, 2, 3]) < 3)[0]] = yx[[False, True, True]] = ynumpy array of booleans: x[np.arange(3) > 0] = yIt also supports:
~dask.array.Array of
booleans: x[x > 0] = y.However, it does not currently support the following:
x[[1, 2, 3], [3, 1, 2]] = y.. _array.assignment.broadcasting:
The normal NumPy broadcasting rules apply:
.. code-block:: python
x = da.zeros((2, 6)) x[0] = 1 x[..., 1] = 2.0 x[:, 2] = [3, 4] x[:, 5:2:-2] = [[6, 5]] x.compute() array([[1., 2., 3., 5., 1., 6.], [0., 2., 4., 5., 0., 6.]]) x[1] = -x[0] x.compute() array([[ 1., 2., 3., 5., 1., 6.], [-1., -2., -3., -5., -1., -6.]])
.. _array.assignment.masking:
Elements may be masked by assigning to the NumPy masked value, or to an array with masked values:
.. code-block:: python
x = da.ones((2, 6)) x[0, [1, -2]] = np.ma.masked x[1] = np.ma.array([0, 1, 2, 3, 4, 5], mask=[0, 1, 1, 0, 0, 0]) print(x.compute()) [[1.0 -- 1.0 1.0 -- 1.0] [0.0 -- -- 3.0 4.0 5.0]] x[:, 0] = x[:, 1] print(x.compute()) [[1.0 -- 1.0 1.0 -- 1.0] [0.0 -- -- 3.0 4.0 5.0]] x[:, 0] = x[:, 1] print(x.compute()) [[-- -- 1.0 1.0 -- 1.0] [-- -- -- 3.0 4.0 5.0]]
If, and only if, a single broadcastable :class:~dask.array.Array of
booleans is provided then masked array assignment does not yet work as
expected. In this case the data underlying the mask are assigned:
.. code-block:: python
x = da.arange(12).reshape(2, 6) x[x > 7] = np.ma.array(-99, mask=True) print(x.compute()) [[ 0 1 2 3 4 5] [ 6 7 -99 -99 -99 -99]]
Note that masked assignments do work when a boolean
:class:~dask.array.Array index used in a tuple, or implicit tuple,
of indices:
.. code-block:: python
x = da.arange(12).reshape(2, 6) x[1, x[0] > 3] = np.ma.masked print(x.compute()) [[0 1 2 3 4 5] [6 7 8 9 -- --]] x = da.arange(12).reshape(2, 6) print(x.compute()) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] x[(x[:, 2] < 4,)] = np.ma.masked print(x.compute()) [[-- -- -- -- -- --] [6 7 8 9 10 11]]