Small example:
df = pd.DataFrame(index=pd.MultiIndex.from_product([['A','B','C'],['a','b']])
Then:
df.loc[:'B'].index.max()
gives ('B', 'b')
as expected.
But:
df.loc[:'B'].index.levels[0].max()
gives 'C'
in conflict with the previous result.
Expected Functionality
The max index of each level matches the max index of the full multiindex (Apologies if this is not actually intended functionality)
Comment From: chris-b1
This behavior is by design - a MultiIndex
is categorical-like, in that it always remembers the possible values (levels
) regardless of if that level is included in a selection.
In [72]: df.index[:0]
Out[72]:
MultiIndex(levels=[['A', 'B', 'C'], ['a', 'b']],
labels=[[], []])
To access the levels actually in a selection, use get_level_values
In [78]: df.loc[:'B'].index.get_level_values(0).max()
Out[78]: 'B'
See docs here http://pandas.pydata.org/pandas-docs/stable/advanced.html#basic-indexing-on-axis-with-multiindex