Skip to content

Commit d206479

Browse files
authored
REF: remove _convert_scalar_indexer (#31962)
1 parent 706e642 commit d206479

File tree

7 files changed

+12
-127
lines changed

7 files changed

+12
-127
lines changed

pandas/core/indexes/base.py

-42
Original file line numberDiff line numberDiff line change
@@ -3078,48 +3078,6 @@ def _get_partial_string_timestamp_match_key(self, key):
30783078
# GH#10331
30793079
return key
30803080

3081-
def _convert_scalar_indexer(self, key, kind: str_t):
3082-
"""
3083-
Convert a scalar indexer.
3084-
3085-
Parameters
3086-
----------
3087-
key : label of the slice bound
3088-
kind : {'loc', 'getitem'}
3089-
"""
3090-
assert kind in ["loc", "getitem"]
3091-
3092-
if len(self) and not isinstance(self, ABCMultiIndex):
3093-
3094-
# we can raise here if we are definitive that this
3095-
# is positional indexing (eg. .loc on with a float)
3096-
# or label indexing if we are using a type able
3097-
# to be represented in the index
3098-
3099-
if kind == "getitem" and is_float(key):
3100-
if not self.is_floating():
3101-
raise KeyError(key)
3102-
3103-
elif kind == "loc" and is_float(key):
3104-
3105-
# we want to raise KeyError on string/mixed here
3106-
# technically we *could* raise a TypeError
3107-
# on anything but mixed though
3108-
if self.inferred_type not in [
3109-
"floating",
3110-
"mixed-integer-float",
3111-
"integer-na",
3112-
"string",
3113-
"mixed",
3114-
]:
3115-
raise KeyError(key)
3116-
3117-
elif kind == "loc" and is_integer(key):
3118-
if not (is_integer_dtype(self.dtype) or is_object_dtype(self.dtype)):
3119-
raise KeyError(key)
3120-
3121-
return key
3122-
31233081
def _validate_positional_slice(self, key: slice):
31243082
"""
31253083
For positional indexing, a slice must have either int or None

pandas/core/indexes/category.py

-10
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,6 @@ def get_indexer_non_unique(self, target):
574574
indexer, missing = self._engine.get_indexer_non_unique(codes)
575575
return ensure_platform_int(indexer), missing
576576

577-
@Appender(Index._convert_scalar_indexer.__doc__)
578-
def _convert_scalar_indexer(self, key, kind: str):
579-
assert kind in ["loc", "getitem"]
580-
if kind == "loc":
581-
try:
582-
return self.categories._convert_scalar_indexer(key, kind="loc")
583-
except TypeError:
584-
raise KeyError(key)
585-
return super()._convert_scalar_indexer(key, kind=kind)
586-
587577
@Appender(Index._convert_list_indexer.__doc__)
588578
def _convert_list_indexer(self, keyarr):
589579
# Return our indexer or raise if all of the values are not included in

pandas/core/indexes/datetimelike.py

-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
is_bool_dtype,
1919
is_categorical_dtype,
2020
is_dtype_equal,
21-
is_float,
2221
is_integer,
2322
is_list_like,
2423
is_period_dtype,
@@ -377,32 +376,6 @@ def _format_attrs(self):
377376
# --------------------------------------------------------------------
378377
# Indexing Methods
379378

380-
def _convert_scalar_indexer(self, key, kind: str):
381-
"""
382-
We don't allow integer or float indexing on datetime-like when using
383-
loc.
384-
385-
Parameters
386-
----------
387-
key : label of the slice bound
388-
kind : {'loc', 'getitem'}
389-
"""
390-
assert kind in ["loc", "getitem"]
391-
392-
if not is_scalar(key):
393-
raise TypeError(key)
394-
395-
# we don't allow integer/float indexing for loc
396-
# we don't allow float indexing for getitem
397-
is_int = is_integer(key)
398-
is_flt = is_float(key)
399-
if kind == "loc" and (is_int or is_flt):
400-
raise KeyError(key)
401-
elif kind == "getitem" and is_flt:
402-
raise KeyError(key)
403-
404-
return super()._convert_scalar_indexer(key, kind=kind)
405-
406379
def _validate_partial_date_slice(self, reso: str):
407380
raise NotImplementedError
408381

pandas/core/indexes/interval.py

-6
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,6 @@ def _should_fallback_to_positional(self):
514514
# positional in this case
515515
return self.dtype.subtype.kind in ["m", "M"]
516516

517-
@Appender(Index._convert_scalar_indexer.__doc__)
518-
def _convert_scalar_indexer(self, key, kind: str):
519-
assert kind in ["getitem", "loc"]
520-
# never iloc, so no-op
521-
return key
522-
523517
def _maybe_cast_slice_bound(self, label, side, kind):
524518
return getattr(self, side)._maybe_cast_slice_bound(label, side, kind)
525519

pandas/core/indexes/numeric.py

-14
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,6 @@ def asi8(self) -> np.ndarray:
254254
# do not cache or you'll create a memory leak
255255
return self.values.view(self._default_dtype)
256256

257-
@Appender(Index._convert_scalar_indexer.__doc__)
258-
def _convert_scalar_indexer(self, key, kind: str):
259-
assert kind in ["loc", "getitem"]
260-
261-
# never iloc, which we don't coerce to integers
262-
key = self._maybe_cast_indexer(key)
263-
return super()._convert_scalar_indexer(key, kind=kind)
264-
265257

266258
class Int64Index(IntegerIndex):
267259
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args
@@ -391,12 +383,6 @@ def astype(self, dtype, copy=True):
391383
def _should_fallback_to_positional(self):
392384
return False
393385

394-
@Appender(Index._convert_scalar_indexer.__doc__)
395-
def _convert_scalar_indexer(self, key, kind: str):
396-
assert kind in ["loc", "getitem"]
397-
# no-op for non-iloc
398-
return key
399-
400386
@Appender(Index._convert_slice_indexer.__doc__)
401387
def _convert_slice_indexer(self, key: slice, kind: str):
402388
assert kind in ["loc", "getitem"]

pandas/core/indexing.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -866,16 +866,7 @@ def _validate_key(self, key, axis: int):
866866
# slice of labels (where start-end in labels)
867867
# slice of integers (only if in the labels)
868868
# boolean
869-
870-
if isinstance(key, slice):
871-
return
872-
873-
if com.is_bool_indexer(key):
874-
return
875-
876-
if not is_list_like_indexer(key):
877-
labels = self.obj._get_axis(axis)
878-
labels._convert_scalar_indexer(key, kind="loc")
869+
pass
879870

880871
def _has_valid_setitem_indexer(self, indexer) -> bool:
881872
return True
@@ -1139,15 +1130,6 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
11391130
if isinstance(key, slice):
11401131
return labels._convert_slice_indexer(key, kind="loc")
11411132

1142-
if is_scalar(key):
1143-
# try to find out correct indexer, if not type correct raise
1144-
try:
1145-
key = labels._convert_scalar_indexer(key, kind="loc")
1146-
except KeyError:
1147-
# but we will allow setting
1148-
if not is_setter:
1149-
raise
1150-
11511133
# see if we are positional in nature
11521134
is_int_index = labels.is_integer()
11531135
is_int_positional = is_integer(key) and not is_int_index
@@ -2029,11 +2011,17 @@ def _convert_key(self, key, is_setter: bool = False):
20292011
if is_setter:
20302012
return list(key)
20312013

2032-
lkey = list(key)
2033-
for n, (ax, i) in enumerate(zip(self.obj.axes, key)):
2034-
lkey[n] = ax._convert_scalar_indexer(i, kind="loc")
2014+
return key
20352015

2036-
return tuple(lkey)
2016+
def __getitem__(self, key):
2017+
if self.ndim != 1 or not is_scalar(key):
2018+
# FIXME: is_scalar check is a kludge
2019+
return super().__getitem__(key)
2020+
2021+
# Like Index.get_value, but we do not allow positional fallback
2022+
obj = self.obj
2023+
loc = obj.index.get_loc(key)
2024+
return obj.index._get_values_for_loc(obj, loc, key)
20372025

20382026

20392027
@Appender(IndexingMixin.iat.__doc__)

pandas/core/series.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,7 @@ def __getitem__(self, key):
852852
return self
853853

854854
key_is_scalar = is_scalar(key)
855-
if key_is_scalar:
856-
key = self.index._convert_scalar_indexer(key, kind="getitem")
857-
elif isinstance(key, (list, tuple)):
855+
if isinstance(key, (list, tuple)):
858856
key = unpack_1tuple(key)
859857

860858
if key_is_scalar or isinstance(self.index, MultiIndex):
@@ -974,8 +972,6 @@ def _get_value(self, label, takeable: bool = False):
974972

975973
# Similar to Index.get_value, but we do not fall back to positional
976974
loc = self.index.get_loc(label)
977-
# We assume that _convert_scalar_indexer has already been called,
978-
# with kind="loc", if necessary, by the time we get here
979975
return self.index._get_values_for_loc(self, loc, label)
980976

981977
def __setitem__(self, key, value):

0 commit comments

Comments
 (0)