Skip to content

DataFrame with MultiIndex raises TypeError instead of KeyError for inexistent keys #27420

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
epassaro opened this issue Jul 16, 2019 · 3 comments · Fixed by #37228
Closed

DataFrame with MultiIndex raises TypeError instead of KeyError for inexistent keys #27420

epassaro opened this issue Jul 16, 2019 · 3 comments · 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

@epassaro
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd

a = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]}

df = pd.DataFrame.from_records(a, index=['A', 'B'])

df.loc[(1,6)]

Problem description

When a non existent key .loc raises TypeError. Is this the intended behavior?

Expected Output

Maybe should raise KeyError

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

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

pandas: 0.24.2
pytest: 4.5.0
pip: 19.0.3
setuptools: 41.0.0
Cython: 0.29.12
numpy: 1.16.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.6.1
sphinx: 1.5.6
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: 3.5.2
numexpr: 2.6.9
feather: None
matplotlib: 3.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.5
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@TomAugspurger
Copy link
Contributor

I think you need to be explicit about where the index labels end. df.loc[(1, 6)] is equivalent to df.loc[1, 6]

In [23]: df.loc[(1, 6), :]

which rasies

~/sandbox/pandas/pandas/core/indexes/multi.py in get_loc_level(self, key, level, drop_level)
   2814                         return (self._engine.get_loc(key), None)
   2815                     except KeyError as e:
-> 2816                         raise KeyError(key) from e
   2817                 else:
   2818                     return partial_selection(key)

@jbrockmendel jbrockmendel added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jul 23, 2019
@jbrockmendel
Copy link
Member

This now raises a KeyError on master

@jbrockmendel jbrockmendel added the Needs Tests Unit test(s) needed to prevent regressions label Oct 15, 2020
@epassaro
Copy link
Author

This now raises a KeyError on master

Yes, that's right.

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.

4 participants