Skip to content

Commit

Permalink
Fixes pydata#4276
Browse files Browse the repository at this point in the history
Pass 0d dask arrays through for indexing.
  • Loading branch information
dcherian committed Jun 24, 2022
1 parent d5e7646 commit 0ab6331
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def __init__(self, key):
raise TypeError(
f"invalid indexer array, does not have integer dtype: {k!r}"
)
if k.ndim != 1:
if k.ndim > 1:
raise TypeError(
f"invalid indexer array for {type(self).__name__}; must have "
f"exactly 1 dimension: {k!r}"
Expand Down
10 changes: 6 additions & 4 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,12 @@ def _broadcast_indexes(self, key):
key = self._item_key_to_tuple(key) # key is a tuple
# key is a tuple of full size
key = indexing.expanded_indexer(key, self.ndim)
# Convert a scalar Variable to an integer
# Convert a scalar Variable to a 0d-array
key = tuple(
k.data.item() if isinstance(k, Variable) and k.ndim == 0 else k for k in key
k.data if isinstance(k, Variable) and k.ndim == 0 else k for k in key
)
# Convert a 0d-array to an integer
# Convert a 0d numpy arrays to an integer
# dask 0d arrays are passed through
key = tuple(
k.item() if isinstance(k, np.ndarray) and k.ndim == 0 else k for k in key
)
Expand Down Expand Up @@ -681,10 +682,11 @@ def _validate_indexers(self, key):
)

def _broadcast_indexes_outer(self, key):
# drop dim if k is integer or if k is a 0d dask array
dims = tuple(
k.dims[0] if isinstance(k, Variable) else dim
for k, dim in zip(key, self.dims)
if not isinstance(k, integer_types)
if (not isinstance(k, integer_types) and k.ndim > 0)
)

new_key = []
Expand Down
3 changes: 1 addition & 2 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,6 @@ def test_indexing_dask_array():
assert_identical(actual, expected)


@pytest.mark.xfail
@requires_dask
def test_indexing_dask_array_scalar():
# GH4276
Expand All @@ -856,7 +855,7 @@ def test_indexing_dask_array_scalar():
da = DataArray(a, dims="x")
x_selector = da.argmax(dim=...)
actual = da.isel(x_selector)
expected = da.isel(x=1)
expected = da.isel(x=-1)
assert_identical(actual, expected)


Expand Down

0 comments on commit 0ab6331

Please sign in to comment.