Skip to content

refactor DaskIndexingAdapter __getitem__ method #8758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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