diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 85229c728848f..7373f41daefa4 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any, List +from typing import Any, List import warnings import numpy as np @@ -29,9 +29,6 @@ from pandas.core.indexes.extension import ExtensionIndex, inherit_names import pandas.core.missing as missing -if TYPE_CHECKING: - from pandas import Series - _index_doc_kwargs = dict(ibase._index_doc_kwargs) _index_doc_kwargs.update(dict(target_klass="CategoricalIndex")) @@ -444,35 +441,6 @@ def _maybe_cast_indexer(self, key): code = self.codes.dtype.type(code) return code - def get_value(self, series: "Series", key: Any): - """ - Fast lookup of value from 1-dimensional ndarray. Only use this if you - know what you're doing - - Parameters - ---------- - series : Series - 1-dimensional array to take values from - key: : scalar - The value of this index at the position of the desired value, - otherwise the positional index of the desired value - - Returns - ------- - Any - The element of the series at the position indicated by the key - """ - k = key - try: - k = self._convert_scalar_indexer(k, kind="getitem") - indexer = self.get_loc(k) - return series.take([indexer])[0] - except (KeyError, TypeError): - pass - - # we might be a positional inexer - return Index.get_value(self, series, key) - @Appender(Index.where.__doc__) def where(self, cond, other=None): # TODO: Investigate an alternative implementation with diff --git a/pandas/core/series.py b/pandas/core/series.py index 0786674daf874..c54331f867a9c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -979,6 +979,9 @@ def _get_value(self, label, takeable: bool = False): """ if takeable: return self._values[label] + + # We assume that _convert_scalar_indexer has already been called, + # with kind="loc", if necessary, by the time we get here return self.index.get_value(self, label) def __setitem__(self, key, value): diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index fa5c75d5e4ad9..bb10b12d94628 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -564,6 +564,18 @@ def test_categorical_assigning_ops(): tm.assert_series_equal(s, exp) +def test_getitem_categorical_str(): + # GH#31765 + ser = pd.Series(range(5), index=pd.Categorical(["a", "b", "c", "a", "b"])) + result = ser["a"] + expected = ser.iloc[[0, 3]] + tm.assert_series_equal(result, expected) + + # Check the intermediate steps work as expected + result = ser.index.get_value(ser, "a") + tm.assert_series_equal(result, expected) + + def test_slice(string_series, object_series): numSlice = string_series[10:20] numSliceEnd = string_series[-10:]