Skip to content

Commit afe4a1e

Browse files
simonjayhawkinsjreback
authored andcommitted
BUG: Incorrect Message in KeyError with MultiIndex (#27291)
1 parent 1e42600 commit afe4a1e

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ Indexing
10391039
10401040
- Improved exception message when calling :meth:`DataFrame.iloc` with a list of non-numeric objects (:issue:`25753`).
10411041
- Improved exception message when calling ``.iloc`` or ``.loc`` with a boolean indexer with different length (:issue:`26658`).
1042+
- Bug in ``KeyError`` exception message when indexing a :class:`MultiIndex` with a non-existant key not displaying the original key (:issue:`27250`).
10421043
- Bug in ``.iloc`` and ``.loc`` with a boolean indexer not raising an ``IndexError`` when too few items are passed (:issue:`26658`).
10431044
- Bug in :meth:`DataFrame.loc` and :meth:`Series.loc` where ``KeyError`` was not raised for a ``MultiIndex`` when the key was less than or equal to the number of levels in the :class:`MultiIndex` (:issue:`14885`).
10441045
- Bug in which :meth:`DataFrame.append` produced an erroneous warning indicating that a ``KeyError`` will be thrown in the future when the data to be appended contains new columns (:issue:`22252`).

pandas/core/indexes/multi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2810,7 +2810,10 @@ def partial_selection(key, indexer=None):
28102810

28112811
if len(key) == self.nlevels and self.is_unique:
28122812
# Complete key in unique index -> standard get_loc
2813-
return (self._engine.get_loc(key), None)
2813+
try:
2814+
return (self._engine.get_loc(key), None)
2815+
except KeyError as e:
2816+
raise KeyError(key) from e
28142817
else:
28152818
return partial_selection(key)
28162819
else:

pandas/tests/indexing/multiindex/test_getitem.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ def test_series_getitem_returns_scalar(
8383
@pytest.mark.parametrize(
8484
"indexer,expected_error,expected_error_msg",
8585
[
86-
(lambda s: s.__getitem__((2000, 3, 4)), KeyError, r"^356$"),
87-
(lambda s: s[(2000, 3, 4)], KeyError, r"^356$"),
88-
(lambda s: s.loc[(2000, 3, 4)], KeyError, r"^356$"),
86+
(lambda s: s.__getitem__((2000, 3, 4)), KeyError, r"^\(2000, 3, 4\)$"),
87+
(lambda s: s[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"),
88+
(lambda s: s.loc[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"),
8989
(lambda s: s.loc[(2000, 3, 4, 5)], IndexingError, "Too many indexers"),
9090
(lambda s: s.__getitem__(len(s)), IndexError, "index out of bounds"),
9191
(lambda s: s[len(s)], IndexError, "index out of bounds"),

pandas/tests/indexing/multiindex/test_loc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def test_loc_getitem_lowerdim_corner(multiindex_dataframe_random_data):
382382
df = multiindex_dataframe_random_data
383383

384384
# test setup - check key not in dataframe
385-
with pytest.raises(KeyError, match=r"^11$"):
385+
with pytest.raises(KeyError, match=r"^\('bar', 'three'\)$"):
386386
df.loc[("bar", "three"), "B"]
387387

388388
# in theory should be inserting in a sorted space????

0 commit comments

Comments
 (0)