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
4 changes: 3 additions & 1 deletion xarray/backends/scipy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def get_variable(self, needs_lock: bool = True) -> scipy.io.netcdf_variable:
def _getitem(self, key):
with self.datastore.lock:
data = self.get_variable(needs_lock=False).data
return data[key]
return indexing.NumpyIndexingAdapter(data).oindex[
indexing.OuterIndexer(key)
]

def __getitem__(self, key):
data = indexing.explicit_indexing_adapter(
Expand Down
31 changes: 31 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4708,6 +4708,37 @@ def test_create_default_indexes(self, tmp_path, create_default_indexes) -> None:
else:
assert len(loaded_ds.xindexes) == 0

def test_indexing_multiple_non_adjacent_indexers(self) -> None:
# GH10338: mixing slices, scalars, and a non-adjacent array indexer
# in .sel() should not shuffle dimension sizes when reading through
# the scipy backend from a closed file object.
data = xr.Dataset(
{
"x": (
("a", "b", "c", "d"),
np.random.rand(3, 6, 5, 25),
)
},
coords={
"a": np.arange(3),
"b": ["u", "v", "w", "x", "y", "z"],
"c": np.arange(5),
"d": np.arange(0, 250, 10),
},
)
with self.roundtrip(data) as on_disk:
actual = on_disk["x"].sel(
b="w",
d=np.arange(0, 250, 20),
)
expected = data["x"].sel(
b="w",
d=np.arange(0, 250, 20),
)
assert actual.dims == expected.dims
assert actual.shape == expected.shape
assert_allclose(actual, expected)


@requires_scipy
class TestScipyFilePath(NetCDF3Only, CFEncodedBase):
Expand Down
Loading