Skip to content

BUG: .loc[] on Series with MultiIndex raises "ValueError: Too many indices" if one level is a tuple #43908

Closed
@andreareina

Description

@andreareina

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
print(pd.__version__)

idx = pd.MultiIndex.from_tuples([(i, 1, (1, 1)) for i in range(5)])
print(pd.Series(range(5), index=idx).loc[(1, 1, (1, 1))])

Issue Description

A Series with a MultiIndex where one level is a tuple cannot be indexed with .loc[]. Instead it raises ValueError: Too many indices:

~/src/pandas $ python test.py 
1.3.3
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print(pd.Series(range(5), index=idx).loc[(1, 1, (1, 1))])
  File "/home/andrea/src/pandas/venv/lib/python3.8/site-packages/pandas/core/indexing.py", line 925, in __getitem__
    return self._getitem_tuple(key)
  File "/home/andrea/src/pandas/venv/lib/python3.8/site-packages/pandas/core/indexing.py", line 1100, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/home/andrea/src/pandas/venv/lib/python3.8/site-packages/pandas/core/indexing.py", line 822, in _getitem_lowerdim
    return self._getitem_nested_tuple(tup)
  File "/home/andrea/src/pandas/venv/lib/python3.8/site-packages/pandas/core/indexing.py", line 881, in _getitem_nested_tuple
    raise ValueError("Too many indices")
ValueError: Too many indices

Expected Behavior

Up to v1.2.5 indexing works as expected to select the data:

~/src/pandas $ python test.py 
1.2.5
1

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.11.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-81-generic
Version : #91~18.04.1-Ubuntu SMP Fri Jul 23 13:36:29 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_SG.UTF-8
LOCALE : en_SG.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.1
setuptools : 56.0.0
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
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions