Skip to content

Commit 24d1657

Browse files
authored
REF: Remove CategoricalIndex.get_value (#31765)
1 parent ca84bd0 commit 24d1657

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

pandas/core/indexes/category.py

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Any, List
1+
from typing import Any, List
22
import warnings
33

44
import numpy as np
@@ -29,9 +29,6 @@
2929
from pandas.core.indexes.extension import ExtensionIndex, inherit_names
3030
import pandas.core.missing as missing
3131

32-
if TYPE_CHECKING:
33-
from pandas import Series
34-
3532
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3633
_index_doc_kwargs.update(dict(target_klass="CategoricalIndex"))
3734

@@ -444,35 +441,6 @@ def _maybe_cast_indexer(self, key):
444441
code = self.codes.dtype.type(code)
445442
return code
446443

447-
def get_value(self, series: "Series", key: Any):
448-
"""
449-
Fast lookup of value from 1-dimensional ndarray. Only use this if you
450-
know what you're doing
451-
452-
Parameters
453-
----------
454-
series : Series
455-
1-dimensional array to take values from
456-
key: : scalar
457-
The value of this index at the position of the desired value,
458-
otherwise the positional index of the desired value
459-
460-
Returns
461-
-------
462-
Any
463-
The element of the series at the position indicated by the key
464-
"""
465-
k = key
466-
try:
467-
k = self._convert_scalar_indexer(k, kind="getitem")
468-
indexer = self.get_loc(k)
469-
return series.take([indexer])[0]
470-
except (KeyError, TypeError):
471-
pass
472-
473-
# we might be a positional inexer
474-
return Index.get_value(self, series, key)
475-
476444
@Appender(Index.where.__doc__)
477445
def where(self, cond, other=None):
478446
# TODO: Investigate an alternative implementation with

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,9 @@ def _get_value(self, label, takeable: bool = False):
979979
"""
980980
if takeable:
981981
return self._values[label]
982+
983+
# We assume that _convert_scalar_indexer has already been called,
984+
# with kind="loc", if necessary, by the time we get here
982985
return self.index.get_value(self, label)
983986

984987
def __setitem__(self, key, value):

pandas/tests/series/indexing/test_indexing.py

+12
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,18 @@ def test_categorical_assigning_ops():
564564
tm.assert_series_equal(s, exp)
565565

566566

567+
def test_getitem_categorical_str():
568+
# GH#31765
569+
ser = pd.Series(range(5), index=pd.Categorical(["a", "b", "c", "a", "b"]))
570+
result = ser["a"]
571+
expected = ser.iloc[[0, 3]]
572+
tm.assert_series_equal(result, expected)
573+
574+
# Check the intermediate steps work as expected
575+
result = ser.index.get_value(ser, "a")
576+
tm.assert_series_equal(result, expected)
577+
578+
567579
def test_slice(string_series, object_series):
568580
numSlice = string_series[10:20]
569581
numSliceEnd = string_series[-10:]

0 commit comments

Comments
 (0)