From 5fe3fa2591a307f5d5489431350ba3915af0439c Mon Sep 17 00:00:00 2001 From: Pietro Battiston Date: Wed, 20 Jun 2018 14:43:45 +0200 Subject: [PATCH 1/2] ERR: Raise a simpler backtrace for missing key --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/core/indexing.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 15c5cc97b8426..40066833b0dc0 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -197,7 +197,7 @@ Strings Indexing ^^^^^^^^ -- +- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorted and clearer (:issue:`21557`) - - diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index d5e81105dd323..38b6aaa2230fb 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -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 @@ -1818,6 +1816,9 @@ def error(): except: error() + if not ax.contains(key): + 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 From 0e90b3005ad74d102ed85b9e3a785d03b1d6a90d Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Thu, 21 Jun 2018 05:41:58 -0400 Subject: [PATCH 2/2] doc --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 40066833b0dc0..a9c49b7476fa6 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -197,7 +197,7 @@ Strings Indexing ^^^^^^^^ -- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorted and clearer (:issue:`21557`) +- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorter and more clear (:issue:`21557`) - -