Skip to content

Commit 9dc61cd

Browse files
committed
ERR: Raise a simpler backtrace for missing key
1 parent b36b451 commit 9dc61cd

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

doc/source/whatsnew/v0.23.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Bug Fixes
6161

6262
- Bug in :meth:`Index.get_indexer_non_unique` with categorical key (:issue:`21448`)
6363
- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with ``nlevels == 1`` (:issue:`21149`)
64+
- The backtrace from a ``KeyError`` is now shorted and clearer (:issue:`21557`)
6465
-
6566

6667
**I/O**

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)