Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
import numpy as np
midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
['speed', 'weight', 'length']],
codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])
df = pd.DataFrame(index=midx, columns=['big', 'small'],
data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
[250, 150], [1.5, 0.8], [320, 250],
[1, 0.8], [0.3, 0.2]])
df.drop(["weight", "length"], level=1, axis=0, inplace=True)
Issue Description
The MultiIndex
changes how expected, however, calling df.index.levels
returns the initial list. I tried v.1.4.4
, v1.5.1
and the last git
, the problem persists.
>>> df.index
MultiIndex([( 'lama', 'speed'),
( 'cow', 'speed'),
('falcon', 'speed')],)
>>> df.index.levels
FrozenList([['lama', 'cow', 'falcon'], ['speed', 'weight', 'length']])
Expected Behavior
I'd expect to see something like that:
>>> df.index.levels
FrozenList([['lama', 'cow', 'falcon'], ['speed']])
Installed Versions
Comment From: phofl
Hi, thanks for your report. This behaves as intended. Could you search the issue tracker for more information? We have quite a few closed issues asking the same/similar questions
Comment From: vkhodygo
Hi @phofl
Thanks for the feedback. Are you referring to, for instance, #49183?
Since it looks like a recurring issue don't you think this should be mentioned somewhere in the docs as expected/intended behaviour?
Comment From: phofl
This is mentioned in the user guide