Skip to content

BUG: MultiIndex.get_loc re-raise TypeError as KeyError #42440

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 9 commits into from
Jul 25, 2021
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ MultiIndex
^^^^^^^^^^
- Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`)
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
- Bug in :meth:`MultiIndex.get_loc` raising ``TypeError`` instead of ``KeyError`` on nested tuple (:issue:`42440`)
-

I/O
Expand Down
14 changes: 11 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2846,9 +2846,17 @@ def _maybe_to_slice(loc):
# needs linear search within the slice
i = self._lexsort_depth
lead_key, follow_key = key[:i], key[i:]
start, stop = (
self.slice_locs(lead_key, lead_key) if lead_key else (0, len(self))
)

if not lead_key:
start = 0
stop = len(self)
else:
try:
start, stop = self.slice_locs(lead_key, lead_key)
except TypeError as err:
# e.g. test_groupby_example key = ((0, 0, 1, 2), "new_col")
# when self has 5 integer levels
raise KeyError(key) from err

if start == stop:
raise KeyError(key)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ def _get_setitem_indexer(self, key):

ax = self.obj._get_axis(0)

if isinstance(ax, MultiIndex) and self.name != "iloc":
with suppress(TypeError, KeyError, InvalidIndexError):
if isinstance(ax, MultiIndex) and self.name != "iloc" and is_hashable(key):
with suppress(KeyError, InvalidIndexError):
# TypeError e.g. passed a bool
return ax.get_loc(key)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import timedelta
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -698,6 +699,14 @@ def test_multiindex_get_loc_list_raises(self):
with pytest.raises(TypeError, match=msg):
idx.get_loc([])

def test_get_loc_nested_tuple_raises_keyerror(self):
# raise KeyError, not TypeError
mi = MultiIndex.from_product([range(3), range(4), range(5), range(6)])
key = ((2, 3, 4), "foo")

with pytest.raises(KeyError, match=re.escape(str(key))):
mi.get_loc(key)


class TestWhere:
def test_where(self):
Expand Down