Skip to content

Support negative step in normalize_indexer#11044

Merged
jsignell merged 9 commits intopydata:mainfrom
avalentino:bugfix/gh-11000
Feb 4, 2026
Merged

Support negative step in normalize_indexer#11044
jsignell merged 9 commits intopydata:mainfrom
avalentino:bugfix/gh-11000

Conversation

@avalentino
Copy link
Contributor

@avalentino avalentino commented Dec 22, 2025

@keewis keewis changed the title Support nevative step in normalize_indexer Support negative step in normalize_indexer Dec 22, 2025
@avalentino
Copy link
Contributor Author

avalentino commented Dec 22, 2025

The failure seems to be unrelated
Unfortunately this patch seems not to fix the rioxarray issue.

Edited: After testing more carefully I can confirm that the patch actually fixes #11000.

Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

This looks good @avalentino sorry it took a while for anyone to review. It slipped through the cracks around the holidays. Do you want to add a section to what's new?

Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

@avalentino
Copy link
Contributor Author

Hi @jsignell , should be fixed now.

>>> _expand_slice(slice(0, -1), 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
"""
sl = normalize_slice(slice_, size)
Copy link
Contributor

Choose a reason for hiding this comment

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

why aren't we using normalize_slice anymore?

Copy link
Member

Choose a reason for hiding this comment

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

Basically this is just using the old version of the function. To me it looks like the alternative would be to catch stop=None and set it to -1 before passing it into np.arange. But slice(*slice_.indices(size)) already returns stop=-1 anyways so 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we have two options: add an option to normalize_slice that allows switching between targeting range or slice (we might need to rename the function then), or just use slice.indices directly. The latter would mean that we'd do this here:

    start, stop, step = slice_.indices(size)
    return np.arange(start, stop, step)

Copy link
Member

Choose a reason for hiding this comment

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

I like the second option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have implemented what @keewis suggestd

Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

This looks good to me. Just a suggestion about the docstring.

@jsignell jsignell enabled auto-merge (squash) February 4, 2026 21:56
@jsignell jsignell merged commit 21153ed into pydata:main Feb 4, 2026
39 checks passed
@welcome
Copy link

welcome bot commented Feb 4, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: normalize_indexer breaks _decompose_slice

4 participants