Skip to content

Commit f1d7cc2

Browse files
Fix empty indexing (#10870)
* fix indexing with empty arrays for OUTER_1VECTOR * add test * add whats-new.rst entry --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent 1c1a674 commit f1d7cc2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ Bug Fixes
3232
~~~~~~~~~
3333
- Fix h5netcdf backend for format=None, use same rule as netcdf4 backend (:pull:`10859`).
3434
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_
35-
3635
- ``netcdf4`` and ``pydap`` backends now use stricter URL detection to avoid incorrectly claiming
3736
remote URLs. The ``pydap`` backend now only claims URLs with explicit DAP protocol indicators
3837
(``dap2://`` or ``dap4://`` schemes, or ``/dap2/`` or ``/dap4/`` in the URL path). This prevents
3938
both backends from claiming remote Zarr stores and other non-DAP URLs without an explicit
4039
``engine=`` argument. (:pull:`10804`). By `Ian Hunt-Isaak <https://github.com/ianhi>`_.
40+
- Fix indexing with empty arrays for scipy & h5netcdf backends which now resolves to empty slices (:issue:`10867`, :pull:`10870`).
41+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_
4142

4243
Documentation
4344
~~~~~~~~~~~~~

xarray/core/indexing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,15 +1366,19 @@ def _decompose_outer_indexer(
13661366
gains = [
13671367
(
13681368
(np.max(k) - np.min(k) + 1.0) / len(np.unique(k))
1369-
if isinstance(k, np.ndarray)
1369+
if isinstance(k, np.ndarray) and k.size != 0
13701370
else 0
13711371
)
13721372
for k in indexer_elems
13731373
]
13741374
array_index = np.argmax(np.array(gains)) if len(gains) > 0 else None
13751375

13761376
for i, (k, s) in enumerate(zip(indexer_elems, shape, strict=False)):
1377-
if isinstance(k, np.ndarray) and i != array_index:
1377+
if isinstance(k, np.ndarray) and k.size == 0:
1378+
# empty np.ndarray key is converted to empty slice
1379+
# see https://github.com/pydata/xarray/issues/10867
1380+
backend_indexer.append(slice(0, 0))
1381+
elif isinstance(k, np.ndarray) and i != array_index:
13781382
# np.ndarray key is converted to slice that covers the entire
13791383
# entries of this key.
13801384
backend_indexer.append(slice(np.min(k), np.max(k) + 1))

xarray/tests/test_backends.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,15 @@ def test_isel_dataarray(self) -> None:
986986
actual = on_disk.isel(dim2=on_disk["dim2"] < 3)
987987
assert_identical(expected, actual)
988988

989+
def test_empty_isel(self) -> None:
990+
# Make sure isel works lazily with empty indexer.
991+
# GH:issue:10867
992+
in_memory = xr.Dataset({"a": ("x", np.arange(4))}, coords={"x": np.arange(4)})
993+
with self.roundtrip(in_memory) as on_disk:
994+
expected = in_memory.isel(x=[])
995+
actual = on_disk.isel(x=[])
996+
assert_identical(expected, actual)
997+
989998
def validate_array_type(self, ds):
990999
# Make sure that only NumpyIndexingAdapter stores a bare np.ndarray.
9911000
def find_and_validate_array(obj):

0 commit comments

Comments
 (0)