Skip to content

Commit f4638af

Browse files
authored
Correct dask handling for 1D idxmax/min on ND data (#4135)
* Correct dask handling for 1D idxmax/min on ND data * Passing black and others * Edit Whats New
1 parent 5121d86 commit f4638af

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ New Features
8080
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:issue:`60`, :pull:`3871`)
8181
By `Todd Jennings <https://github.com/toddrjen>`_
8282
- Support dask handling for :py:meth:`DataArray.idxmax`, :py:meth:`DataArray.idxmin`,
83-
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:pull:`3922`)
84-
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
83+
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:pull:`3922`, :pull:`4135`)
84+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Pascal Bourgault <https://github.com/aulemahal>`_.
8585
- More support for unit aware arrays with pint (:pull:`3643`, :pull:`3975`)
8686
By `Justus Magin <https://github.com/keewis>`_.
8787
- Support overriding existing variables in ``to_zarr()`` with ``mode='a'`` even

xarray/core/computation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ def _calc_idxminmax(
15631563

15641564
chunks = dict(zip(array.dims, array.chunks))
15651565
dask_coord = dask.array.from_array(array[dim].data, chunks=chunks[dim])
1566-
res = indx.copy(data=dask_coord[(indx.data,)])
1566+
res = indx.copy(data=dask_coord[indx.data.ravel()].reshape(indx.shape))
15671567
# we need to attach back the dim name
15681568
res.name = dim
15691569
else:

xarray/tests/test_dataarray.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,6 +5257,25 @@ def test_idxmax(self, x, minindex, maxindex, nanindex, use_dask):
52575257
assert_identical(result7, expected7)
52585258

52595259

5260+
class TestReduceND(TestReduce):
5261+
@pytest.mark.parametrize("op", ["idxmin", "idxmax"])
5262+
@pytest.mark.parametrize("ndim", [3, 5])
5263+
def test_idxminmax_dask(self, op, ndim):
5264+
if not has_dask:
5265+
pytest.skip("requires dask")
5266+
5267+
ar0_raw = xr.DataArray(
5268+
np.random.random_sample(size=[10] * ndim),
5269+
dims=[i for i in "abcdefghij"[: ndim - 1]] + ["x"],
5270+
coords={"x": np.arange(10)},
5271+
attrs=self.attrs,
5272+
)
5273+
5274+
ar0_dsk = ar0_raw.chunk({})
5275+
# Assert idx is the same with dask and without
5276+
assert_equal(getattr(ar0_dsk, op)(dim="x"), getattr(ar0_raw, op)(dim="x"))
5277+
5278+
52605279
@pytest.fixture(params=[1])
52615280
def da(request):
52625281
if request.param == 1:

0 commit comments

Comments
 (0)