-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: Inconsistent handling of dropping levels in MultiIndex when using IndexSlice #47596
Comments
Thanks @mousey92 for the report.
Firstly, in the versions you mention The second case, The way the indexing works, is it can sometime look for a match in the columns Index to allow partial indexing. So In conclusion, including a So for instance We may already have an issue about this. |
Note that it is not about the first level, just about the slice in the nested tuple. print(
df.loc[
("A", 0, slice(None)),
]
)
# a b
# 0 1 0 1
# A 0 a 0.526977 0.658588 0.771332 0.852590
# b 0.084024 0.761542 0.850593 0.916064
print(
df.loc[
("A", 0),
]
)
# a b
# 0 1 0 1
# a 0.526977 0.658588 0.771332 0.852590
# b 0.084024 0.761542 0.850593 0.916064 |
seems like a few. I think #10552 covers this issue succinctly, so closing as duplicate. |
I didn't find #10552 before, but yes that issue covers this and this can be closed as duplicate. |
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
Not 100% sure this issue has not been reported yet. #10521 seems to discuss the same issue, but in my opinion doesn't generalize the problem well enough.
When selecting from the DataFrame using .loc and a scalar value, the level that scalar value is indexing is dropped. But when selecting the same using IndexSlice, the behaviour is unpredictable (see examples). Depending on which and how much levels are 'sliced' using a scalar, the level of that scalar selection may be dropped, but it is unclear what the rules for this are. Especially since there are some mentions in the documentation and other issues of scalars always dropping the level.
Expected Behavior
Any index level that is selected using a scalar value from that level should either have the level dropped across index selection tools, or not have the level dropped across index selection tools. Either way, it should be consistent.
Installed Versions
INSTALLED VERSIONS
commit : e8093ba
python : 3.10.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Netherlands.1252
pandas : 1.4.3
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.1.0
pip : 22.1.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 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: