-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: inconsisten multi-level indexing when levels are dropped #10521
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
pls show the frame u r indexing as well as pd.show_versions() |
Here is a simplified example. In the real scenario I am using a selector of the form [idx1, :, idx2] where the resulting DataFrame may have one or several values for the second level. df = pd.DataFrame({'a': list('abcd'), 'b': ['a','b']*2, 'c': ['a']*4, 'd':0})
df = df.set_index(['a', 'b', 'c']).sortlevel()
print df
print df.loc['a',:,'a']
df_ = df[:1]
print df_
print df_.loc['a',:,'a']
|
This is hitting an 'older' piece of code that I haven't really touched (well afraid is more like it :) The solution is to basically use slicers, which always give you back the original dimension (the full dims). These are like Note that you MUST fully specify all dimensions (e.g. index & columns)
I would actually write this like this, with the
So I guess this is a bug. Want to dive in? |
I'm happy with the slicer notation, though I hope others don't have to figure this out by backtracking mysterious behavior in their code. |
yep, this is an older wart, which requires removing quite of bit of older code (and then fixing anything that the 'newer' way doesn't support). Not that hard, but requires some effort. |
DF.loc['A', :, 1]
returns a DataFrame with the full MultiIndex if the second level has more than one entry, and a truncated index if the second level has only one entry (same as, e.g.,DF.loc['A',0,1]
). Is this the intended behavior?The text was updated successfully, but these errors were encountered: