Skip to content

fix: preserve RangeIndex.arange step#11328

Open
jaythehardcoder wants to merge 1 commit into
pydata:mainfrom
jaythehardcoder:fix-rangeindex-arange-step
Open

fix: preserve RangeIndex.arange step#11328
jaythehardcoder wants to merge 1 commit into
pydata:mainfrom
jaythehardcoder:fix-rangeindex-arange-step

Conversation

@jaythehardcoder
Copy link
Copy Markdown

Description

RangeIndex.arange currently computes the transform size from (stop - start) / step, but leaves RangeCoordinateTransform.step to be derived from (stop - start) / size. When step does 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 by 0.25 instead of matching np.arange(0.0, 1.0, 0.3).

This preserves the requested step on RangeIndex.arange transforms and propagates the logical spacing through slice-derived transforms.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Hermes Agent / ChatGPT

Test Plan

  • uv run pytest xarray/tests/test_range_index.py -q
  • uv run ruff check xarray/indexes/range_index.py xarray/tests/test_range_index.py

Closes #11325

@welcome
Copy link
Copy Markdown

welcome Bot commented May 9, 2026

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

transform = RangeCoordinateTransform(
start, stop, size, coord_name, dim, dtype=dtype
)
transform._step = step
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning a different value than would be computed to a property's cache seems brittle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RangeIndex.arange does not preserve step, has different values than numpy.arange

2 participants