-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: loc
with MultiIndex
as index is returning incorrect Index
#46704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
loc
when MultiIndex
is returning incorrect Index
loc
with MultiIndex
as index is returning incorrect Index
I think this is not a bug. compare this with singleIndex case, or compare with |
Agreed @kwhkim, this is documented in e.g. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html. Is there documentation that lead you to believe the level should not be dropped @galipremsagar? |
Thanks @galipremsagar for the report
This was a bug fixed in commit: [35b338e] BUG: .loc failing to drop first level (#42435) but unfortunately there was no issue opened related to this to allow discussion or even a release note to communicate the change in behavior. Note: there does still seem to be some inconsistent behavior and #42435 mentioned that there are other issues.
and for DataFrame too
so it appears that you can still get the old behavior with s.loc[(1, slice(None)),] for now. There was a test added, test_loc_drops_level in #42435 to test for this new behavior, so I think we can just close this issue? |
Thanks for tracking that down @simonjayhawkins; @galipremsagar - sorry, I didn't notice you pointed to two different versions of pandas in the OP. As an alternative to maintain the level, one can use
Does that work for your use case? |
Yep, I agree that the behavior is more consistent now. Although the last two cases from @simonjayhawkins are still unfortunate |
let's leave issue open for now. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
It appears to be that one level is being dropped when
loc
is called.Expected Behavior
Installed Versions
The text was updated successfully, but these errors were encountered: