We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
MultiIndex
.loc
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
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.
import pandas as pd import numpy as np df = pd.DataFrame(index=pd.MultiIndex.from_product([list('ab'), list('cd'), list('e')]), columns=['Val']) print(df.loc[np.s_[:,'c',:]]) print(df.loc[np.s_[:,'c',:], "Val"]) print(df.loc[np.s_[:,'c',:], :])
When only selecting the index, the second level is dropped. But if I add a selector to the columns, the second level is kept.
I'm not sure if this is intended behavior, but it's inconsistant. FYI, I have a relevent report earlier: #43599
commit : a671b5a python : 3.10.6.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-89-generic Version : #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8
pandas : 2.1.4 numpy : 1.26.2 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 65.5.1 pip : 23.3.2 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader : None bs4 : None bottleneck : None dataframe-api-compat: None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None
The text was updated successfully, but these errors were encountered:
Note in the second example, the column "Val" is also dropped. So I think maybe the "c" in the index should also be dropped.
Sorry, something went wrong.
Thanks for the report. This appears to be a duplicate of #18631. Closing for now - if you think I've missed something, comment here and we can reopen.
No branches or pull requests
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
When only selecting the index, the second level is dropped. But if I add a selector to the columns, the second level is kept.

Expected Behavior
I'm not sure if this is intended behavior, but it's inconsistant. FYI, I have a relevent report earlier: #43599
Installed Versions
INSTALLED VERSIONS
commit : a671b5a
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-89-generic
Version : #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.4
numpy : 1.26.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 65.5.1
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: