1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
From 5c54e8b62ec1b0526daf33bb34bceb3a62d04fc8 Mon Sep 17 00:00:00 2001
From: Antonio Valentino <antonio.valentino@tiscali.it>
Date: Mon, 22 Dec 2025 01:34:19 +0100
Subject: [PATCH] Support nevative step in normalize_indexer
---
xarray/core/indexing.py | 7 ++++---
xarray/tests/test_indexing.py | 3 ++-
2 files changed, 6 insertions(+), 4 deletions(-)
--- a/xarray/core/indexing.py
+++ b/xarray/core/indexing.py
@@ -257,7 +257,8 @@
>>> 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]]:
@@ -280,14 +281,14 @@
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:
--- a/xarray/tests/test_indexing.py
+++ b/xarray/tests/test_indexing.py
@@ -282,10 +282,11 @@
(-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):
|