Fix MultiIndex sel with tuple-valued levels (GH#11341)#11411
Open
C1-BA-B1-F3 wants to merge 1 commit into
Open
Fix MultiIndex sel with tuple-valued levels (GH#11341)#11411C1-BA-B1-F3 wants to merge 1 commit into
C1-BA-B1-F3 wants to merge 1 commit into
Conversation
Problem: When a MultiIndex level contains tuple-valued entries (e.g., (1,1)), selecting with a nested tuple key like ((1,1), 2) incorrectly preserved the dimension instead of collapsing it to a scalar result. Root cause: _is_nested_tuple() was checking for 'tuple' in addition to 'list' and 'slice', which caused it to misidentify tuple-valued keys as nested selection tuples. Fix: Remove 'tuple' from the isinstance check in _is_nested_tuple() so that only 'list' and 'slice' are treated as indicators of nested selections. Tuple- valued keys in MultiIndex levels are now correctly handled as scalar key values. Added regression test for selecting with nested tuple keys on MultiIndex with tuple-valued levels.
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.
Problem
When a MultiIndex level contains tuple-valued entries (e.g.,
(1,1)), selecting with a nested tuple key like((1,1), 2)incorrectly preserves the dimension instead of collapsing it to a scalar result.Example from Issue #11341
Root Cause
The
_is_nested_tuple()function was checking fortuplein addition tolistandslice:This caused tuple-valued keys like
((1,1), 2)to be incorrectly identified as nested selection tuples (containing multiple selection values), when they are actually single keys with a tuple-valued first level.Fix
Remove
tuplefrom the isinstance check so onlylistandsliceare treated as indicators of nested selections:This correctly distinguishes:
(1, 2)→ single key (scalar values) → useget_loc()((1,1), 2)→ single key (tuple value in first level) → useget_loc()([1,2], 3)→ nested selection (list) → useget_locs()(slice(1,2), 3)→ nested selection (slice) → useget_locs()Tests
Added regression test
test_sel_nested_tuple_keythat verifies:Closes #11341