fix: preserve RangeIndex.arange step#11328
Open
jaythehardcoder wants to merge 2 commits into
Open
Conversation
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
cfriedland5
reviewed
May 9, 2026
| transform = RangeCoordinateTransform( | ||
| start, stop, size, coord_name, dim, dtype=dtype | ||
| ) | ||
| transform._step = step |
There was a problem hiding this comment.
Assigning a different value than would be computed to a property's cache seems brittle
Replaced direct _step cache assignment with canonical stop computation, so the step property naturally produces the requested value via its own formula (stop - start) / size. Reviewer cfriedland5 correctly flagged that assigning a different value than the property would compute to a private cache field (transform._step = step) is brittle. The fix computes stop = start + size * step where size is already determined by ceil((stop - start) / step), so the step property's formula returns the requested step without touching private state. Updated the equals_exact and isel tests to match the new consistent stop behavior — arange and slicing now produce the same stop when parameters align, so exact equality holds. Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
RangeIndex.arangecurrently computes the transform size from(stop - start) / step, but leavesRangeCoordinateTransform.stepto be derived from(stop - start) / size. Whenstepdoes not evenly divide the interval, that changes the materialized coordinate values.For example,
RangeIndex.arange(0.0, 1.0, 0.3, dim="x")currently produces coordinates spaced by0.25instead of matchingnp.arange(0.0, 1.0, 0.3).This preserves the requested
steponRangeIndex.arangetransforms and propagates the logical spacing through slice-derived transforms.Checklist
whats-new.rstapi.rstAI Disclosure
Tools: Hermes Agent / ChatGPT
Test Plan
uv run pytest xarray/tests/test_range_index.py -quv run ruff check xarray/indexes/range_index.py xarray/tests/test_range_index.pyCloses #11325