Skip to content

Commit

Permalink
Correct dask handling for 1D idxmax/min on ND data (#4135)
Browse files Browse the repository at this point in the history
* Correct dask handling for 1D idxmax/min on ND data

* Passing black and others

* Edit Whats New
  • Loading branch information
aulemahal authored Jun 25, 2020
1 parent 5121d86 commit f4638af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ New Features
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:issue:`60`, :pull:`3871`)
By `Todd Jennings <https://github.com/toddrjen>`_
- Support dask handling for :py:meth:`DataArray.idxmax`, :py:meth:`DataArray.idxmin`,
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:pull:`3922`)
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:pull:`3922`, :pull:`4135`)
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Pascal Bourgault <https://github.com/aulemahal>`_.
- More support for unit aware arrays with pint (:pull:`3643`, :pull:`3975`)
By `Justus Magin <https://github.com/keewis>`_.
- Support overriding existing variables in ``to_zarr()`` with ``mode='a'`` even
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ def _calc_idxminmax(

chunks = dict(zip(array.dims, array.chunks))
dask_coord = dask.array.from_array(array[dim].data, chunks=chunks[dim])
res = indx.copy(data=dask_coord[(indx.data,)])
res = indx.copy(data=dask_coord[indx.data.ravel()].reshape(indx.shape))
# we need to attach back the dim name
res.name = dim
else:
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5257,6 +5257,25 @@ def test_idxmax(self, x, minindex, maxindex, nanindex, use_dask):
assert_identical(result7, expected7)


class TestReduceND(TestReduce):
@pytest.mark.parametrize("op", ["idxmin", "idxmax"])
@pytest.mark.parametrize("ndim", [3, 5])
def test_idxminmax_dask(self, op, ndim):
if not has_dask:
pytest.skip("requires dask")

ar0_raw = xr.DataArray(
np.random.random_sample(size=[10] * ndim),
dims=[i for i in "abcdefghij"[: ndim - 1]] + ["x"],
coords={"x": np.arange(10)},
attrs=self.attrs,
)

ar0_dsk = ar0_raw.chunk({})
# Assert idx is the same with dask and without
assert_equal(getattr(ar0_dsk, op)(dim="x"), getattr(ar0_raw, op)(dim="x"))


@pytest.fixture(params=[1])
def da(request):
if request.param == 1:
Expand Down

0 comments on commit f4638af

Please sign in to comment.