BUG: Accessing a one-level MultiIndex with .loc
raises with an unclear error (in 1.4)
#45779
Closed
3 tasks done
Labels
Bug
Indexing
Related to indexing on series/frames, not to indexes themselves
Regression
Functionality that used to work in a prior pandas version
Milestone
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
Accessing a row of a DataFrame indexed using a
MultiIndex
with only one level using theloc
method raises aValueError: Cannot remove 1 levels from an index with 1 levels: at least one level must be left.
.This change of behaviour does not seem to be listed in the breaking changes section of the changelog, and I haven't find any warning related to this usage in the documentation.
Expected Behavior
With pandas 1.3.5,
df.loc['a
]` returns the subpart of the DataFrame where the index is 'a':Installed Versions
pandas : 1.4.0
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
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.0.2
IPython : 7.29.0
pandas_datareader: None
bs4 : None
bottleneck : 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 : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: