Skip to content

Commit 582323b

Browse files
toobazjreback
authored andcommitted
ERR: Raise a simpler backtrace for missing key (#21558)
1 parent f4fba9e commit 582323b

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Strings
197197
Indexing
198198
^^^^^^^^
199199

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

pandas/core/indexing.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1807,8 +1807,6 @@ def error():
18071807

18081808
try:
18091809
key = self._convert_scalar_indexer(key, axis)
1810-
if not ax.contains(key):
1811-
error()
18121810
except TypeError as e:
18131811

18141812
# python 3 type errors should be raised
@@ -1818,6 +1816,9 @@ def error():
18181816
except:
18191817
error()
18201818

1819+
if not ax.contains(key):
1820+
error()
1821+
18211822
def _is_scalar_access(self, key):
18221823
# this is a shortcut accessor to both .loc and .iloc
18231824
# that provide the equivalent access of .at and .iat

0 commit comments

Comments
 (0)