Skip to content
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: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ Bug Fixes
~~~~~~~~~
- Fix h5netcdf backend for format=None, use same rule as netcdf4 backend (:pull:`10859`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_

- ``netcdf4`` and ``pydap`` backends now use stricter URL detection to avoid incorrectly claiming
remote URLs. The ``pydap`` backend now only claims URLs with explicit DAP protocol indicators
(``dap2://`` or ``dap4://`` schemes, or ``/dap2/`` or ``/dap4/`` in the URL path). This prevents
both backends from claiming remote Zarr stores and other non-DAP URLs without an explicit
``engine=`` argument. (:pull:`10804`). By `Ian Hunt-Isaak <https://github.com/ianhi>`_.
- Fix indexing with empty arrays for scipy & h5netcdf backends which now resolves to empty slices (:issue:`10867`, :pull:`10870`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 6 additions & 2 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,15 +1366,19 @@ def _decompose_outer_indexer(
gains = [
(
(np.max(k) - np.min(k) + 1.0) / len(np.unique(k))
if isinstance(k, np.ndarray)
if isinstance(k, np.ndarray) and k.size != 0
else 0
)
for k in indexer_elems
]
array_index = np.argmax(np.array(gains)) if len(gains) > 0 else None

for i, (k, s) in enumerate(zip(indexer_elems, shape, strict=False)):
if isinstance(k, np.ndarray) and i != array_index:
if isinstance(k, np.ndarray) and k.size == 0:
# empty np.ndarray key is converted to empty slice
# see https://github.com/pydata/xarray/issues/10867
backend_indexer.append(slice(0, 0))
elif isinstance(k, np.ndarray) and i != array_index:
# np.ndarray key is converted to slice that covers the entire
# entries of this key.
backend_indexer.append(slice(np.min(k), np.max(k) + 1))
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ def test_isel_dataarray(self) -> None:
actual = on_disk.isel(dim2=on_disk["dim2"] < 3)
assert_identical(expected, actual)

def test_empty_isel(self) -> None:
# Make sure isel works lazily with empty indexer.
# GH:issue:10867
in_memory = xr.Dataset({"a": ("x", np.arange(4))}, coords={"x": np.arange(4)})
with self.roundtrip(in_memory) as on_disk:
expected = in_memory.isel(x=[])
actual = on_disk.isel(x=[])
assert_identical(expected, actual)

def validate_array_type(self, ds):
# Make sure that only NumpyIndexingAdapter stores a bare np.ndarray.
def find_and_validate_array(obj):
Expand Down
Loading