Skip to content

MultiIndex.get_loc throws KeyError under specific circumstances #30053

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

Closed
p-himik opened this issue Dec 4, 2019 · 1 comment · Fixed by #37228
Closed

MultiIndex.get_loc throws KeyError under specific circumstances #30053

p-himik opened this issue Dec 4, 2019 · 1 comment · Fixed by #37228
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@p-himik
Copy link

p-himik commented Dec 4, 2019

Code Sample, a copy-pastable example if possible

from pandas import MultiIndex

key = ('a', 7)
idx = MultiIndex(levels=[['a'], [0, 7], [1]],
                 codes=[[0, 0], [1, 0], [0, 0]],
                 names=['x', 'y', 'z'],
                 sortorder=0)

idx.get_loc(key)

Problem description

Executing the above code results in

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-19-26c5d4d0888a> in <module>
----> 1 idx.get_loc(key)

~/soft/miniconda3/envs/junk/lib/python3.7/site-packages/pandas/core/indexes/multi.py in get_loc(self, key, method)
   2696 
   2697         if start == stop:
-> 2698             raise KeyError(key)
   2699 
   2700         if not follow_key:

KeyError: ('a', 1, 2)

Note that the exception disappears if you do any of the following:

  • change sortorder to None
  • change the codes of level 1 to [0, 1]
  • change the values of level 1 to [0, 7]
  • completely remove level 2

Expected Output

I don't think there should be any exception here since the way I created the index appears to be valid, and the key is definitely in there.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-23-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.15.4
pytz : 2018.9
dateutil : 2.7.5
pip : 18.1
setuptools : 40.6.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : 7.2.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.0.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Feb 25, 2020
@jbrockmendel
Copy link
Member

This works on master, could use a test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants