diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index c34efe325c7..783a08e0449 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -269,7 +269,8 @@ def normalize_slice(sl: slice, size: int) -> slice: >>> normalize_slice(slice(0, -1), 10) slice(0, 9, 1) """ - return slice(*sl.indices(size)) + start, stop, step = sl.indices(size) + return slice(start, stop if stop >= 0 else None, step) def _expand_slice(slice_: slice, size: int) -> np.ndarray[Any, np.dtype[np.integer]]: @@ -292,14 +293,14 @@ def slice_slice(old_slice: slice, applied_slice: slice, size: int) -> slice: index it with another slice to return a new slice equivalent to applying the slices sequentially """ - old_slice = normalize_slice(old_slice, size) + old_slice = slice(*old_slice.indices(size)) size_after_old_slice = len(range(old_slice.start, old_slice.stop, old_slice.step)) if size_after_old_slice == 0: # nothing left after applying first slice return slice(0) - applied_slice = normalize_slice(applied_slice, size_after_old_slice) + applied_slice = slice(*applied_slice.indices(size_after_old_slice)) start = old_slice.start + applied_slice.start * old_slice.step if start < 0: diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index dfb3283a16e..7f525a0615f 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -299,10 +299,11 @@ class TestLazyArray: (-1, 3, 2), (slice(None), 4, slice(0, 4, 1)), (slice(1, -3), 7, slice(1, 4, 1)), + (slice(None, None, -1), 8, slice(7, None, -1)), (np.array([-1, 3, -2]), 5, np.array([4, 3, 3])), ), ) - def normalize_indexer(self, indexer, size, expected): + def test_normalize_indexer(self, indexer, size, expected): actual = indexing.normalize_indexer(indexer, size) if isinstance(expected, np.ndarray):