Skip to content

Commit 56401a7

Browse files
topper-123tm9k1
authored andcommitted
clean CategoricalIndex.get_loc (pandas-dev#23091)
1 parent 7983923 commit 56401a7

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pandas/core/indexes/category.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ def get_loc(self, key, method=None):
426426
-------
427427
loc : int if unique index, slice if monotonic index, else mask
428428
429+
Raises
430+
------
431+
KeyError : if the key is not in the index
432+
429433
Examples
430434
---------
431435
>>> unique_index = pd.CategoricalIndex(list('abc'))
@@ -440,10 +444,11 @@ def get_loc(self, key, method=None):
440444
>>> non_monotonic_index.get_loc('b')
441445
array([False, True, False, True], dtype=bool)
442446
"""
443-
codes = self.categories.get_loc(key)
444-
if (codes == -1):
447+
code = self.categories.get_loc(key)
448+
try:
449+
return self._engine.get_loc(code)
450+
except KeyError:
445451
raise KeyError(key)
446-
return self._engine.get_loc(codes)
447452

448453
def get_value(self, series, key):
449454
"""

0 commit comments

Comments
 (0)