Skip to content

ERR: Raise a simpler backtrace for missing key #21558

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

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Strings
Indexing
^^^^^^^^

-
- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorter and more clear (:issue:`21557`)
-
-

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,8 +1807,6 @@ def error():

try:
key = self._convert_scalar_indexer(key, axis)
if not ax.contains(key):
error()
except TypeError as e:

# python 3 type errors should be raised
Expand All @@ -1818,6 +1816,9 @@ def error():
except:
error()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my last few commits this except clause hid the cause of the underlying exception. Cant it just be removed altogether and let exceptions raise as they occur onstead of goinc through error()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my last few commits this except clause hid the cause of the underlying exception.

Well, in principle this is its exact goal ;-) Can you provide an example? I guess we should just specify the exceptions it catches.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant it just be removed altogether and let exceptions raise as they occur onstead of goinc through error()?

@topper-123 well, maybe you're right!


if not ax.contains(key):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add a test for this (maybe compare the exception message)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately comparing the message is not enough (it didn't change - and is already tested). We would have to check the traceback, and I have no idea of how to do it - and if we want to do it.

error()

def _is_scalar_access(self, key):
# this is a shortcut accessor to both .loc and .iloc
# that provide the equivalent access of .at and .iat
Expand Down