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

KeyError error message could be less confusing #21557

Closed
toobaz opened this issue Jun 20, 2018 · 0 comments · Fixed by #21558
Closed

KeyError error message could be less confusing #21557

toobaz opened this issue Jun 20, 2018 · 0 comments · Fixed by #21558
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@toobaz
Copy link
Member

toobaz commented Jun 20, 2018

Code Sample, a copy-pastable example if possible

In [2]: pd.Series(range(3)).loc[4]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1810                 if not ax.contains(key):
-> 1811                     error()
   1812             except TypeError as e:

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 

KeyError: 'the label [4] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-2-a40b2ecc79ee> in <module>()
----> 1 pd.Series(range(3)).loc[4]

/home/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
   1497 
   1498             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1499             return self._getitem_axis(maybe_callable, axis=axis)
   1500 
   1501     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1930 
   1931         # fall thru to straight lookup
-> 1932         self._validate_key(key, axis)
   1933         return self._get_label(key, axis=axis)
   1934 

/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1817                 raise
   1818             except:
-> 1819                 error()
   1820 
   1821     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1804                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 
   1808             try:

KeyError: 'the label [4] is not in the [index]'

Problem description

The way in which we catch exceptions is uninteresting to the user. The sequence of two exceptions suggests to the user that there is a bug in the code, or unexpected exception, which is not the case; so this error message is confusing.

The fix is trivial.

Expected Output

In [2]: pd.Series(range(3)).loc[4]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-a40b2ecc79ee> in <module>()
----> 1 pd.Series(range(3)).loc[4]

/home/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
   1497 
   1498             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1499             return self._getitem_axis(maybe_callable, axis=axis)
   1500 
   1501     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1931 
   1932         # fall thru to straight lookup
-> 1933         self._validate_key(key, axis)
   1934         return self._get_label(key, axis=axis)
   1935 

/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1818 
   1819             if not ax.contains(key):
-> 1820                 error()
   1821 
   1822     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1804                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 
   1808             try:

KeyError: 'the label [4] is not in the [index]'

Output of pd.show_versions()

INSTALLED VERSIONS

commit: b36b451
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-6-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.24.0.dev0+132.gb36b451a7
pytest: 3.5.0
pip: 9.0.1
setuptools: 39.2.0
Cython: 0.25.2
numpy: 1.14.3
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.0dev
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.2.2.post1153+gff6786446
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.3.0
xlsxwriter: 0.9.6
lxml: 4.1.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1

@toobaz toobaz added Indexing Related to indexing on series/frames, not to indexes themselves Error Reporting Incorrect or improved errors from pandas labels Jun 20, 2018
@jreback jreback added this to the 0.24.0 milestone Jun 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants