Skip to content

Fix RangeIndex slicing #10471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
slicing impl: rely on range
This also enables support for reverse slicing (added tests)
  • Loading branch information
benbovy committed Jul 1, 2025
commit ea6742cac4cb16f3b1a68f2f01076e5bda6a95a2
14 changes: 5 additions & 9 deletions xarray/indexes/range_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from xarray.core.coordinate_transform import CoordinateTransform
from xarray.core.dataarray import DataArray
from xarray.core.indexes import CoordinateTransformIndex, Index, PandasIndex
from xarray.core.indexing import IndexSelResult, normalize_slice
from xarray.core.indexing import IndexSelResult
from xarray.core.variable import Variable


Expand Down Expand Up @@ -79,14 +79,10 @@ def equals(
)

def slice(self, sl: slice) -> "RangeCoordinateTransform":
sl = normalize_slice(sl, self.size)

# TODO: support reverse transform (i.e., start > stop)?
assert sl.start < sl.stop

new_size = (sl.stop - sl.start - 1) // sl.step + 1
new_start = self.start + sl.start * self.step
new_stop = min(new_start + new_size * sl.step * self.step, self.stop)
new_range = range(self.size)[sl]
new_size = len(new_range)
new_start = self.start + new_range.start * self.step
new_stop = self.start + new_range.stop * self.step

return type(self)(
new_start, new_stop, new_size, self.coord_name, self.dim, dtype=self.dtype
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_range_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def test_range_index_isel() -> None:
expected = create_dataset_arange(0.0, 1.0, 0.2)
assert_identical(actual, expected, check_default_indexes=False)

actual = ds.isel(x=slice(None, None, -1))
expected = create_dataset_arange(0.9, -0.1, -0.1)
assert_identical(actual, expected, check_default_indexes=False)

actual = ds.isel(x=slice(None, 4, -1))
expected = create_dataset_arange(0.9, 0.4, -0.1)
assert_identical(actual, expected, check_default_indexes=False)

actual = ds.isel(x=slice(8, 4, -1))
expected = create_dataset_arange(0.8, 0.4, -0.1)
assert_identical(actual, expected, check_default_indexes=False)

actual = ds.isel(x=slice(8, None, -1))
expected = create_dataset_arange(0.8, -0.1, -0.1)
assert_identical(actual, expected, check_default_indexes=False)

# https://github.com/pydata/xarray/issues/10441
ds2 = create_dataset_arange(0.0, 3.0, 0.1)
actual = ds2.isel(x=slice(4, None, 3))
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy