-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: MultiIndex.levels[x] of sliced DataFrame returns values from the non-sliced index #55148
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
Comments
Thanks for the report. MultiIndex makes no guarantee that the levels are only those which are present in the values. Users can create a MultiIndex such as
where I think it's expected that the levels have more than just the values that are present. When pandas slices, it only updates the codes for performance.There is remove_unused_levels available. |
Thanks @rhshadrach, good to know. I've added this info to the StackOverflow question. |
Given the creation of two bug reports on this topic, it's probably worth noting this design choice explicitly in the |
That sounds like a good idea to me @trianta2. Would you be interested in putting up a PR for this? |
I'll close this in favor of #55435 which I think it's more accurate on what needs to be done. |
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
I have a
DataFrame
with aMultiIndex
and slice it by a subset of its level 0 values using.loc[]
. When I call.index.levels[0]
on the slicedDataFrame
, the resulting index also includes values from the original, pre-sliced index. However theDataFrame
is correctly sliced. See also this StackOverflow question from 2014.Expected Behavior
the levels of the
MultiIndex
should be aligned with the actual values in theDataFrame
.Installed Versions
INSTALLED VERSIONS
commit : 965ceca
python : 3.10.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.90.1-microsoft-standard-WSL2
Version : #1 SMP Fri Jan 27 02:56:13 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.2
numpy : 1.25.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.2.1
Cython : None
pytest : 7.3.2
hypothesis : 6.79.2
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
brotli : 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
snappy : 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: