Skip to content

Commit 5bf7aa2

Browse files
Fix axis reordering bug in scipy backend outer indexing (#11638)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ede38e5 commit 5bf7aa2

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎xarray/backends/scipy_.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def get_variable(self, needs_lock: bool = True) -> scipy.io.netcdf_variable:
9191
def _getitem(self, key):
9292
with self.datastore.lock:
9393
data = self.get_variable(needs_lock=False).data
94-
return data[key]
94+
return indexing.NumpyIndexingAdapter(data).oindex[
95+
indexing.OuterIndexer(key)
96+
]
9597

9698
def __getitem__(self, key):
9799
data = indexing.explicit_indexing_adapter(

‎xarray/tests/test_backends.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,37 @@ def test_create_default_indexes(self, tmp_path, create_default_indexes) -> None:
47084708
else:
47094709
assert len(loaded_ds.xindexes) == 0
47104710

4711+
def test_indexing_multiple_non_adjacent_indexers(self) -> None:
4712+
# GH10338: mixing slices, scalars, and a non-adjacent array indexer
4713+
# in .sel() should not shuffle dimension sizes when reading through
4714+
# the scipy backend from a closed file object.
4715+
data = xr.Dataset(
4716+
{
4717+
"x": (
4718+
("a", "b", "c", "d"),
4719+
np.random.rand(3, 6, 5, 25),
4720+
)
4721+
},
4722+
coords={
4723+
"a": np.arange(3),
4724+
"b": ["u", "v", "w", "x", "y", "z"],
4725+
"c": np.arange(5),
4726+
"d": np.arange(0, 250, 10),
4727+
},
4728+
)
4729+
with self.roundtrip(data) as on_disk:
4730+
actual = on_disk["x"].sel(
4731+
b="w",
4732+
d=np.arange(0, 250, 20),
4733+
)
4734+
expected = data["x"].sel(
4735+
b="w",
4736+
d=np.arange(0, 250, 20),
4737+
)
4738+
assert actual.dims == expected.dims
4739+
assert actual.shape == expected.shape
4740+
assert_allclose(actual, expected)
4741+
47114742

47124743
@requires_scipy
47134744
class TestScipyFilePath(NetCDF3Only, CFEncodedBase):

0 commit comments

Comments
 (0)