1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
.. _array.assignment:
Assignment
==========
Dask Array supports most of the NumPy assignment indexing syntax. In
particular, it supports combinations of the following:
* Indexing by integers: ``x[1] = y``
* Indexing by slices: ``x[2::-1] = y``
* Indexing by a list of integers: ``x[[0, -1, 1]] = y``
* Indexing by a 1-d :class:`numpy` array of integers: ``x[np.arange(3)] = y``
* Indexing by a 1-d :class:`~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]] = y``
* Indexing by a list of booleans: ``x[[False, True, True]] = y``
* Indexing by a 1-d :class:`numpy` array of booleans: ``x[np.arange(3) > 0] = y``
It also supports:
* Indexing by one broadcastable :class:`~dask.array.Array` of
booleans: ``x[x > 0] = y``.
However, it does not currently support the following:
* Indexing with lists in multiple axes: ``x[[1, 2, 3], [3, 1, 2]] = y``
.. _array.assignment.broadcasting:
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:
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]]
|