Skip to content

Keep empty negative-step slices empty on lazily indexed arrays - #11983

Open
pinjun99 wants to merge 2 commits into
pydata:mainfrom
pinjun99:oss/11981
Open

pinjun99 wants to merge 2 commits into
pydata:mainfrom
pinjun99:oss/11981

Conversation

@pinjun99

@pinjun99 pinjun99 commented Dec 4, 2026

Copy link
Copy Markdown

Description

Empty slices with a negative step on lazily indexed backend arrays now select nothing, as in NumPy.

  • _decompose_slice returns slice(0, 0), slice(None) when the negative-step range is empty, instead of reaching range(...)[-1] (the IndexError for slice(1, 1, -1)).
  • normalize_slice returns slice(0, 0, 1) when slice.indices gives a start of -1. That only happens for an empty negative-step slice that starts before the axis (for example slice(-4, None, -1) on 3 elements). Before, the result was slice(-1, None, -1), which the lazy wrapper then read as "the whole axis, reversed".

Tests: new cases in test_normalize_indexer and test_decompose_slice, plus test_lazily_indexed_empty_negative_step_slice, which runs both slices from the issue through LazilyIndexedArray on a BackendArray with IndexingSupport.BASIC. All of them fail on main and pass with this change. Locally, xarray/tests/test_indexing.py gives 175 passed, 8 skipped. The two TestMemoryCachedArray async tests fail with and without this change because pytest-asyncio isn't installed in my environment. ruff check and ruff format --check pass on the changed files.

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: Codex (first draft of the fix and the unit-test cases; I added the end-to-end lazy-array test and reran the tests myself).

@welcome

welcome Bot commented Dec 4, 2026

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
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.

Empty negative-step slices on lazily indexed backend arrays: IndexError, and the whole axis since 2026.2

1 participant