Skip to content

Commit 64e87b3

Browse files
authored
BUG: better exception on invalid slicing on CategoricalIndex (#40364)
1 parent d0ec737 commit 64e87b3

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

pandas/core/indexes/category.py

-7
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,6 @@ def _convert_list_indexer(self, keyarr):
542542

543543
return self.get_indexer_for(keyarr)
544544

545-
@doc(Index._maybe_cast_slice_bound)
546-
def _maybe_cast_slice_bound(self, label, side: str, kind):
547-
if kind == "loc":
548-
return label
549-
550-
return super()._maybe_cast_slice_bound(label, side, kind)
551-
552545
# --------------------------------------------------------------------
553546

554547
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:

pandas/tests/indexing/test_categorical.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,11 @@ def test_ix_categorical_index_non_unique(self):
469469

470470
def test_loc_slice(self):
471471
# GH9748
472-
with pytest.raises(KeyError, match="1"):
472+
msg = (
473+
"cannot do slice indexing on CategoricalIndex with these "
474+
r"indexers \[1\] of type int"
475+
)
476+
with pytest.raises(TypeError, match=msg):
473477
self.df.loc[1:5]
474478

475479
result = self.df.loc["b":"c"]

0 commit comments

Comments
 (0)