Skip to content
Open
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
7 changes: 4 additions & 3 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading