Rework PandasMultiIndex.sel internals #7004
Open
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.
whats-new.rst
This PR hopefully improves how are handled the labels that are provided for multi-index level coordinates in
.sel()
.More specifically, slices are handled in a cleaner way and it is now allowed to provide array-like labels.
PandasMultiIndex.sel()
relies on the underlyingpandas.MultiIndex
methods like this:get_loc
when all levels are provided with each a scalar label (no slice, no array)get_loc_level
when only a subset of levels are provided with scalar labels onlyget_locs
for all other cases.This yields a predictable behavior: as soon as one of the provided labels is a slice or array-like, the multi-index and all its level coordinates are kept in the result.
Some cases illustrated below (I compare this PR with an older release due to the errors reported in #6838):