Skip to content

Commit

Permalink
refactor DaskIndexingAdapter __getitem__ method (#8758)
Browse files Browse the repository at this point in the history
* remove the unnecessary check for `VectorizedIndexer`

* remvove test

* update whats-new

* update test

* Trim

---------

Co-authored-by: Deepak Cherian <deepak@cherian.net>
  • Loading branch information
andersy005 and dcherian authored Feb 15, 2024
1 parent 5d93331 commit ff0d056
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 24 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ Internal Changes
- Adds :py:func:`open_datatree` into ``xarray/backends`` (:pull:`8697`) By `Matt
Savoie <https://github.com/flamingbear>`_.

- Refactor :py:meth:`xarray.core.indexing.DaskIndexingAdapter.__getitem__` to remove an unnecessary rewrite of the indexer key
(:issue: `8377`, :pull:`8758`) By `Anderson Banihirwe <https://github.com/andersy005>`

.. _whats-new.2024.01.1:

v2024.01.1 (23 Jan, 2024)
Expand Down
19 changes: 1 addition & 18 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import functools
import operator
from collections import Counter, defaultdict
from collections.abc import Hashable, Iterable, Mapping
from collections.abc import Hashable, Mapping
from contextlib import suppress
from dataclasses import dataclass, field
from datetime import timedelta
Expand Down Expand Up @@ -1418,23 +1418,6 @@ def __init__(self, array):
self.array = array

def __getitem__(self, key):
if not isinstance(key, VectorizedIndexer):
# if possible, short-circuit when keys are effectively slice(None)
# This preserves dask name and passes lazy array equivalence checks
# (see duck_array_ops.lazy_array_equiv)
rewritten_indexer = False
new_indexer = []
for idim, k in enumerate(key.tuple):
if isinstance(k, Iterable) and (
not is_duck_dask_array(k)
and duck_array_ops.array_equiv(k, np.arange(self.array.shape[idim]))
):
new_indexer.append(slice(None))
rewritten_indexer = True
else:
new_indexer.append(k)
if rewritten_indexer:
key = type(key)(tuple(new_indexer))

if isinstance(key, BasicIndexer):
return self.array[key.tuple]
Expand Down
6 changes: 0 additions & 6 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,16 +1662,10 @@ def test_lazy_array_equiv_merge(compat):
lambda a: a.assign_attrs(new_attr="anew"),
lambda a: a.assign_coords(cxy=a.cxy),
lambda a: a.copy(),
lambda a: a.isel(x=np.arange(a.sizes["x"])),
lambda a: a.isel(x=slice(None)),
lambda a: a.loc[dict(x=slice(None))],
lambda a: a.loc[dict(x=np.arange(a.sizes["x"]))],
lambda a: a.loc[dict(x=a.x)],
lambda a: a.sel(x=a.x),
lambda a: a.sel(x=a.x.values),
lambda a: a.transpose(...),
lambda a: a.squeeze(), # no dimensions to squeeze
lambda a: a.sortby("x"), # "x" is already sorted
lambda a: a.reindex(x=a.x),
lambda a: a.reindex_like(a),
lambda a: a.rename({"cxy": "cnew"}).rename({"cnew": "cxy"}),
Expand Down

0 comments on commit ff0d056

Please sign in to comment.