Skip to content
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: .loc[] on Series with MultiIndex raises "ValueError: Too many indices" if one level is a tuple #43908

Closed
2 of 3 tasks
andreareina opened this issue Oct 7, 2021 · 2 comments · Fixed by #44035
Closed
2 of 3 tasks
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@andreareina
Copy link

  • 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

@andreareina andreareina added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 7, 2021
@mroeschke
Copy link
Member

This looks to work on master. Could use a test

In [1]: 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))])
1.4.0.dev0+878.g2aa3ffef18
1

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 13, 2021
@KoenSR
Copy link
Contributor

KoenSR commented Oct 13, 2021

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants