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

/usr/lib/python3.10/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.") INSTALLED VERSIONS ------------------ commit : 62c0fb880014c098df194777e7aee22613c5f6e0 python : 3.10.8.final.0 python-bits : 64 OS : Linux OS-release : 6.0.6-arch1-1 Version : #1 SMP PREEMPT_DYNAMIC Sat, 29 Oct 2022 14:08:39 +0000 machine : x86_64 processor : byteorder : little LC_ALL : en_GB.UTF-8 LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8 pandas : 2.0.0.dev0+574.g62c0fb88001 numpy : 1.23.4 pytz : 2022.1 dateutil : 2.8.2 setuptools : 64.0.0 pip : 22.3 Cython : 0.29.32 pytest : 7.2.0 hypothesis : 6.54.4 sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.9.1 html5lib : 1.1 pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.6.0 pandas_datareader: None bs4 : 4.11.1 bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.6.2 numba : None numexpr : None odfpy : None openpyxl : 3.0.10 pandas_gbq : None pyarrow : 6.0.1 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.9.3 snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : None

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