Repository navigation
Fix RangeIndex.isel with negative positions and boolean masks - #11986
Open
parabol123654 wants to merge 1 commit into
Open
parabol123654 wants to merge 1 commit into
parabol123654 wants to merge 1 commit into
Conversation
RangeIndex.isel computes the labels of the new PandasIndex as
start + key * step, using the raw array key. Negative positions are not
wrapped and a boolean mask is used as 0/1 instead of being turned into
positions, so the coordinate no longer matches the data:
ds.isel(x=[-1, -2]) # x = [-1., -2.], not [4., 3.]
ds.isel(x=np.array([False, True, True])) # x has 3 labels, data has 2
ds.isel(x=xr.DataArray(mask, dims="x")) # ValueError: conflicting sizes
The data variables are indexed correctly, only the index is wrong. This
has been there since RangeIndex was added in pydata#10076.
Convert a boolean key with np.flatnonzero and wrap negative positions
by the index size before calling the transform, like numpy does. This
doesn't build the full range, so the index stays cheap.
Co-authored-by: Claude <noreply@anthropic.com>
parabol123654
force-pushed
the
fix-range-index-isel
branch
from
December 4, 2026 18:13
532c3c4 to
ee91ca3
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ds.isel(x=[-1, -2])on a dimension with aRangeIndexgivesx = [-1., -2.]instead of[4., 3.], and a boolean mask gives labels like[0., 1., 0., 1., 1.](mask times step), which then doesn't match the length of the data. With aDataArraymask it fails with "conflicting sizes".RangeIndex.iselpasses the raw key to the transform, so I now convert boolean keys withnp.flatnonzeroand wrap negative positions before that, without materializing the range.The new test fails on main and passes with the fix;
test_range_index.py,test_indexes.py,test_coordinate_transform.pyandtest_indexing.pypass.Checklist
whats-new.rstAI Disclosure
Tools: Claude Code. It found the bug while checking RangeIndex indexing against numpy and wrote the fix and test; I reproduced it, read the change and reran the tests myself.
[This is Claude Code on behalf of Manh Nguyen]