Skip to content

Commit 728b648

Browse files
authored
use the DaskIndexingAdapter for duck dask arrays (#6414)
* use the DaskIndexingAdapter for all duck-dask-arrays * add tests covering the change * use force_ndarray_like to allow duck arrays in quantities * fix the test ids * also test all combinations with dask * fix whats-new.rst * whats-new.rst entry
1 parent 8a2fbb8 commit 728b648

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

Diff for: doc/whats-new.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ New Features
2727
`Christian Jauvin <https://github.com/cjauvin>`_ and `David Huard <https://github.com/huard>`_.
2828
- Add a ``create_index=True`` parameter to :py:meth:`Dataset.stack` and
2929
:py:meth:`DataArray.stack` so that the creation of multi-indexes is optional
30-
(:pull:`5692`). By `Benoît Bovy <https://github.com/benbovy>`_.
30+
(:pull:`5692`).
31+
By `Benoît Bovy <https://github.com/benbovy>`_.
3132
- Multi-index levels are now accessible through their own, regular coordinates
3233
instead of virtual coordinates (:pull:`5692`).
3334
By `Benoît Bovy <https://github.com/benbovy>`_.
@@ -36,7 +37,8 @@ Breaking changes
3637
~~~~~~~~~~~~~~~~
3738

3839
- The Dataset and DataArray ``rename*`` methods do not implicitly add or drop
39-
indexes. (:pull:`5692`). By `Benoît Bovy <https://github.com/benbovy>`_.
40+
indexes. (:pull:`5692`).
41+
By `Benoît Bovy <https://github.com/benbovy>`_.
4042
- Many arguments like ``keep_attrs``, ``axis``, and ``skipna`` are now keyword
4143
only for all reduction operations like ``.mean``.
4244
By `Deepak Cherian <https://github.com/dcherian>`_, `Jimmy Westling <https://github.com/illviljan>`_.
@@ -50,12 +52,16 @@ Bug fixes
5052

5153
- Set ``skipna=None`` for all ``quantile`` methods (e.g. :py:meth:`Dataset.quantile`) and
5254
ensure it skips missing values for float dtypes (consistent with other methods). This should
53-
not change the behavior (:pull:`6303`). By `Mathias Hauser <https://github.com/mathause>`_.
55+
not change the behavior (:pull:`6303`).
56+
By `Mathias Hauser <https://github.com/mathause>`_.
5457
- Many bugs fixed by the explicit indexes refactor, mainly related to multi-index (virtual)
5558
coordinates. See the corresponding pull-request on GitHub for more details. (:pull:`5692`).
5659
By `Benoît Bovy <https://github.com/benbovy>`_.
5760
- Fixed "unhashable type" error trying to read NetCDF file with variable having its 'units'
58-
attribute not ``str`` (e.g. ``numpy.ndarray``) (:issue:`6368`). By `Oleh Khoma <https://github.com/okhoma>`_.
61+
attribute not ``str`` (e.g. ``numpy.ndarray``) (:issue:`6368`).
62+
By `Oleh Khoma <https://github.com/okhoma>`_.
63+
- Allow fancy indexing of duck dask arrays along multiple dimensions. (:pull:`6414`)
64+
By `Justus Magin <https://github.com/keewis>`_.
5965

6066
Documentation
6167
~~~~~~~~~~~~~
@@ -68,7 +74,6 @@ Internal Changes
6874
corresponding pull-request on GitHub for more details. (:pull:`5692`).
6975
By `Benoît Bovy <https://github.com/benbovy>`_.
7076

71-
.. _whats-new.2022.02.0:
7277
.. _whats-new.2022.03.0:
7378

7479
v2022.03.0 (2 March 2022)

Diff for: xarray/core/indexing.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@
2525

2626
from . import duck_array_ops, nputils, utils
2727
from .npcompat import DTypeLike
28-
from .pycompat import (
29-
dask_array_type,
30-
dask_version,
31-
integer_types,
32-
is_duck_dask_array,
33-
sparse_array_type,
34-
)
28+
from .pycompat import dask_version, integer_types, is_duck_dask_array, sparse_array_type
3529
from .types import T_Xarray
3630
from .utils import either_dict_or_kwargs, get_valid_numpy_dtype
3731

@@ -682,7 +676,7 @@ def as_indexable(array):
682676
return NumpyIndexingAdapter(array)
683677
if isinstance(array, pd.Index):
684678
return PandasIndexingAdapter(array)
685-
if isinstance(array, dask_array_type):
679+
if is_duck_dask_array(array):
686680
return DaskIndexingAdapter(array)
687681
if hasattr(array, "__array_function__"):
688682
return NdArrayLikeIndexingAdapter(array)

Diff for: xarray/tests/test_units.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
# make sure scalars are converted to 0d arrays so quantities can
3333
# always be treated like ndarrays
34-
unit_registry = pint.UnitRegistry(force_ndarray=True)
34+
unit_registry = pint.UnitRegistry(force_ndarray_like=True)
3535
Quantity = unit_registry.Quantity
3636

3737

@@ -1837,21 +1837,43 @@ def test_broadcast_equals(self, unit, dtype):
18371837

18381838
assert expected == actual
18391839

1840+
@pytest.mark.parametrize("dask", [False, pytest.param(True, marks=[requires_dask])])
18401841
@pytest.mark.parametrize(
1841-
"indices",
1842+
["variable", "indexers"],
18421843
(
1843-
pytest.param(4, id="single index"),
1844-
pytest.param([5, 2, 9, 1], id="multiple indices"),
1844+
pytest.param(
1845+
xr.Variable("x", np.linspace(0, 5, 10)),
1846+
{"x": 4},
1847+
id="single value-single indexer",
1848+
),
1849+
pytest.param(
1850+
xr.Variable("x", np.linspace(0, 5, 10)),
1851+
{"x": [5, 2, 9, 1]},
1852+
id="multiple values-single indexer",
1853+
),
1854+
pytest.param(
1855+
xr.Variable(("x", "y"), np.linspace(0, 5, 20).reshape(4, 5)),
1856+
{"x": 1, "y": 4},
1857+
id="single value-multiple indexers",
1858+
),
1859+
pytest.param(
1860+
xr.Variable(("x", "y"), np.linspace(0, 5, 20).reshape(4, 5)),
1861+
{"x": [0, 1, 2], "y": [0, 2, 4]},
1862+
id="multiple values-multiple indexers",
1863+
),
18451864
),
18461865
)
1847-
def test_isel(self, indices, dtype):
1848-
array = np.linspace(0, 5, 10).astype(dtype) * unit_registry.s
1849-
variable = xr.Variable("x", array)
1866+
def test_isel(self, variable, indexers, dask, dtype):
1867+
if dask:
1868+
variable = variable.chunk({dim: 2 for dim in variable.dims})
1869+
quantified = xr.Variable(
1870+
variable.dims, variable.data.astype(dtype) * unit_registry.s
1871+
)
18501872

18511873
expected = attach_units(
1852-
strip_units(variable).isel(x=indices), extract_units(variable)
1874+
strip_units(quantified).isel(indexers), extract_units(quantified)
18531875
)
1854-
actual = variable.isel(x=indices)
1876+
actual = quantified.isel(indexers)
18551877

18561878
assert_units_equal(expected, actual)
18571879
assert_identical(expected, actual)

0 commit comments

Comments
 (0)