Skip to content

raise error on slice-based selection of multi-index levels#11168

Merged
dcherian merged 1 commit intopydata:mainfrom
Mr-Neutr0n:fix-multiindex-level-slice
Feb 13, 2026
Merged

raise error on slice-based selection of multi-index levels#11168
dcherian merged 1 commit intopydata:mainfrom
Mr-Neutr0n:fix-multiindex-level-slice

Conversation

@Mr-Neutr0n
Copy link
Contributor

Fixes #10534.

.sel(level=slice(...)) on a multi-index level was silently returning wrong results — the slice got passed straight to pandas get_loc_level, which interpreted it as a range query on just that one level. Depending on the data, this either returns garbage results or an unhelpful KeyError.

Now it raises a ValueError with a message pointing the user to use scalar values instead. Added a test covering both cases.

previously, calling .sel(level=slice(...)) on a multi-index level
would silently produce wrong results by passing the slice directly
to pandas get_loc_level, which interprets it as a range query on that
level alone. now raises a ValueError with a clear message instead.

closes pydata#10534
Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

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

Nice PR. Thank you!

@dcherian dcherian enabled auto-merge (squash) February 13, 2026 21:05
@dcherian dcherian merged commit 2908dd4 into pydata:main Feb 13, 2026
39 checks passed
@welcome
Copy link

welcome bot commented Feb 13, 2026

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

ianhi added a commit to ianhi/xarray that referenced this pull request Feb 13, 2026
Co-authored-by: Claude <noreply@anthropic.com>
ianhi added a commit to ianhi/xarray that referenced this pull request Feb 13, 2026
Co-authored-by: Claude <noreply@anthropic.com>
ianhi added a commit that referenced this pull request Feb 13, 2026
Co-authored-by: Claude <noreply@anthropic.com>
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.

Slicing a MultiIndex level is broken

2 participants