Skip to content

Commit 0d078c5

Browse files
authored
CLN: _convert_scalar_indexer only handle "loc" and "getitem" (#31709)
1 parent fdba416 commit 0d078c5

File tree

6 files changed

+21
-27
lines changed

6 files changed

+21
-27
lines changed

pandas/core/indexes/base.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -3100,20 +3100,16 @@ def _filter_indexer_tolerance(
31003100
# --------------------------------------------------------------------
31013101
# Indexer Conversion Methods
31023102

3103-
def _convert_scalar_indexer(self, key, kind=None):
3103+
def _convert_scalar_indexer(self, key, kind: str_t):
31043104
"""
31053105
Convert a scalar indexer.
31063106
31073107
Parameters
31083108
----------
31093109
key : label of the slice bound
3110-
kind : {'loc', 'getitem', 'iloc'} or None
3110+
kind : {'loc', 'getitem'}
31113111
"""
3112-
assert kind in ["loc", "getitem", "iloc", None]
3113-
3114-
if kind == "iloc":
3115-
self._validate_indexer("positional", key, "iloc")
3116-
return key
3112+
assert kind in ["loc", "getitem"]
31173113

31183114
if len(self) and not isinstance(self, ABCMultiIndex):
31193115

pandas/core/indexes/category.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,11 @@ def get_indexer_non_unique(self, target):
624624
return ensure_platform_int(indexer), missing
625625

626626
@Appender(Index._convert_scalar_indexer.__doc__)
627-
def _convert_scalar_indexer(self, key, kind=None):
627+
def _convert_scalar_indexer(self, key, kind: str):
628+
assert kind in ["loc", "getitem"]
628629
if kind == "loc":
629630
try:
630-
return self.categories._convert_scalar_indexer(key, kind=kind)
631+
return self.categories._convert_scalar_indexer(key, kind="loc")
631632
except TypeError:
632633
self._invalid_indexer("label", key)
633634
return super()._convert_scalar_indexer(key, kind=kind)

pandas/core/indexes/datetimelike.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,18 @@ def _format_attrs(self):
385385
# --------------------------------------------------------------------
386386
# Indexing Methods
387387

388-
def _convert_scalar_indexer(self, key, kind=None):
388+
def _convert_scalar_indexer(self, key, kind: str):
389389
"""
390390
We don't allow integer or float indexing on datetime-like when using
391391
loc.
392392
393393
Parameters
394394
----------
395395
key : label of the slice bound
396-
kind : {'loc', 'getitem', 'iloc'} or None
396+
kind : {'loc', 'getitem'}
397397
"""
398398

399-
assert kind in ["loc", "getitem", "iloc", None]
399+
assert kind in ["loc", "getitem"]
400400

401401
if not is_scalar(key):
402402
raise TypeError(key)

pandas/core/indexes/interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,9 @@ def _should_fallback_to_positional(self):
529529
return self.dtype.subtype.kind in ["m", "M"]
530530

531531
@Appender(Index._convert_scalar_indexer.__doc__)
532-
def _convert_scalar_indexer(self, key, kind=None):
533-
if kind == "iloc":
534-
return super()._convert_scalar_indexer(key, kind=kind)
532+
def _convert_scalar_indexer(self, key, kind: str):
533+
assert kind in ["getitem", "loc"]
534+
# never iloc, so no-op
535535
return key
536536

537537
def _maybe_cast_slice_bound(self, label, side, kind):

pandas/core/indexes/numeric.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,11 @@ def asi8(self) -> np.ndarray:
250250
return self.values.view(self._default_dtype)
251251

252252
@Appender(Index._convert_scalar_indexer.__doc__)
253-
def _convert_scalar_indexer(self, key, kind=None):
254-
assert kind in ["loc", "getitem", "iloc", None]
253+
def _convert_scalar_indexer(self, key, kind: str):
254+
assert kind in ["loc", "getitem"]
255255

256-
# don't coerce ilocs to integers
257-
if kind != "iloc":
258-
key = self._maybe_cast_indexer(key)
256+
# never iloc, which we don't coerce to integers
257+
key = self._maybe_cast_indexer(key)
259258
return super()._convert_scalar_indexer(key, kind=kind)
260259

261260

@@ -388,12 +387,9 @@ def _should_fallback_to_positional(self):
388387
return False
389388

390389
@Appender(Index._convert_scalar_indexer.__doc__)
391-
def _convert_scalar_indexer(self, key, kind=None):
392-
assert kind in ["loc", "getitem", "iloc", None]
393-
394-
if kind == "iloc":
395-
self._validate_indexer("positional", key, "iloc")
396-
390+
def _convert_scalar_indexer(self, key, kind: str):
391+
assert kind in ["loc", "getitem"]
392+
# no-op for non-iloc
397393
return key
398394

399395
@Appender(Index._convert_slice_indexer.__doc__)

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
20332033
return labels._convert_slice_indexer(key, kind="iloc")
20342034

20352035
elif is_float(key):
2036-
return labels._convert_scalar_indexer(key, kind="iloc")
2036+
labels._validate_indexer("positional", key, "iloc")
2037+
return key
20372038

20382039
self._validate_key(key, axis)
20392040
return key

0 commit comments

Comments
 (0)