Registry Mirror
Skip to content

Conversation

@lowgame

@lowgame lowgame commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Description

RangeIndex.arange() could report a negative dimension size when the interval direction conflicted with the sign of step:

index = xr.indexes.RangeIndex.arange(0.0, 3.0, -1.0, dim="x")
index.size  # -3

The equivalent numpy.arange(0.0, 3.0, -1.0) is empty. The negative metadata also prevented the generated coordinate from being attached to an ordinary empty DataArray.

The size calculation used ceil((stop - start) / step) directly, so wrong-direction intervals produced a negative count. This change clamps that count to zero before normalizing stop. Regression tests cover both ascending and descending wrong-direction intervals and verify the index size, coordinate size, and empty DataArray integration.

Checklist

  • Tests added
  • User visible changes are documented in whats-new.rst
  • New functions/methods are listed in api.rst (not applicable)

Test results

  • Regression before the production fix: uv run --no-dev --with pytest --with pytest-env --with pytest-timeout --with pytest-asyncio --with 'pytest-mypy-plugins>=4.0.0' pytest xarray/tests/test_range_index.py::test_range_index_arange_empty -q — 2 failed as expected (index.size was -3).
  • uv run --no-dev --with pytest --with pytest-env --with pytest-timeout --with pytest-asyncio --with 'pytest-mypy-plugins>=4.0.0' pytest xarray/tests/test_range_index.py -q — 29 passed.
  • Same environment, pytest xarray/tests/test_indexes.py xarray/tests/test_range_index.py -q — 107 passed, 2 skipped.
  • uv run --no-dev --with pre-commit pre-commit run --files xarray/indexes/range_index.py xarray/tests/test_range_index.py doc/whats-new.rst — passed.
  • Minimal-dependency pytest xarray -q — 6776 passed, 9998 skipped, 12 xfailed, 4 xpassed, 2 failed. Both failures reproduce unchanged on the base commit: a pandas-dependent categorical repr byte-count mismatch and NDPointIndex requiring scipy, which was intentionally absent from this minimal environment.

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested the AI-generated content in this PR.
    • I take responsibility for the AI-generated content in this PR.
      Tools: OpenAI Codex

Prepared by OpenAI Codex on behalf of @lowgame; the defect was reproduced before the production change, the complete diff was reviewed, and the commands above were run locally.

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thx

@headtr1ck headtr1ck added the plan to merge Final call for comments label Sep 28, 2026
@dcherian
dcherian merged commit a6c1dfa into pydata:main Sep 28, 2026
57 checks passed
@welcome

welcome Bot commented Sep 28, 2026

Copy link
Copy Markdown

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

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

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants