diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 3c735fc0309b6..e431d0bcf7e9b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3100,20 +3100,16 @@ def _filter_indexer_tolerance( # -------------------------------------------------------------------- # Indexer Conversion Methods - def _convert_scalar_indexer(self, key, kind=None): + def _convert_scalar_indexer(self, key, kind: str_t): """ Convert a scalar indexer. Parameters ---------- key : label of the slice bound - kind : {'loc', 'getitem', 'iloc'} or None + kind : {'loc', 'getitem'} """ - assert kind in ["loc", "getitem", "iloc", None] - - if kind == "iloc": - self._validate_indexer("positional", key, "iloc") - return key + assert kind in ["loc", "getitem"] if len(self) and not isinstance(self, ABCMultiIndex): diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 2eda54ec8d4ed..85229c728848f 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -624,10 +624,11 @@ def get_indexer_non_unique(self, target): return ensure_platform_int(indexer), missing @Appender(Index._convert_scalar_indexer.__doc__) - def _convert_scalar_indexer(self, key, kind=None): + def _convert_scalar_indexer(self, key, kind: str): + assert kind in ["loc", "getitem"] if kind == "loc": try: - return self.categories._convert_scalar_indexer(key, kind=kind) + return self.categories._convert_scalar_indexer(key, kind="loc") except TypeError: self._invalid_indexer("label", key) return super()._convert_scalar_indexer(key, kind=kind) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 13fb955c32832..b143ff0aa9c02 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -385,7 +385,7 @@ def _format_attrs(self): # -------------------------------------------------------------------- # Indexing Methods - def _convert_scalar_indexer(self, key, kind=None): + def _convert_scalar_indexer(self, key, kind: str): """ We don't allow integer or float indexing on datetime-like when using loc. @@ -393,10 +393,10 @@ def _convert_scalar_indexer(self, key, kind=None): Parameters ---------- key : label of the slice bound - kind : {'loc', 'getitem', 'iloc'} or None + kind : {'loc', 'getitem'} """ - assert kind in ["loc", "getitem", "iloc", None] + assert kind in ["loc", "getitem"] if not is_scalar(key): raise TypeError(key) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 9ec72df140c85..03fb8db2e1e1e 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -529,9 +529,9 @@ def _should_fallback_to_positional(self): return self.dtype.subtype.kind in ["m", "M"] @Appender(Index._convert_scalar_indexer.__doc__) - def _convert_scalar_indexer(self, key, kind=None): - if kind == "iloc": - return super()._convert_scalar_indexer(key, kind=kind) + def _convert_scalar_indexer(self, key, kind: str): + assert kind in ["getitem", "loc"] + # never iloc, so no-op return key def _maybe_cast_slice_bound(self, label, side, kind): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 2f4c48cc2e5a5..d67c40a78d807 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -250,12 +250,11 @@ def asi8(self) -> np.ndarray: return self.values.view(self._default_dtype) @Appender(Index._convert_scalar_indexer.__doc__) - def _convert_scalar_indexer(self, key, kind=None): - assert kind in ["loc", "getitem", "iloc", None] + def _convert_scalar_indexer(self, key, kind: str): + assert kind in ["loc", "getitem"] - # don't coerce ilocs to integers - if kind != "iloc": - key = self._maybe_cast_indexer(key) + # never iloc, which we don't coerce to integers + key = self._maybe_cast_indexer(key) return super()._convert_scalar_indexer(key, kind=kind) @@ -388,12 +387,9 @@ def _should_fallback_to_positional(self): return False @Appender(Index._convert_scalar_indexer.__doc__) - def _convert_scalar_indexer(self, key, kind=None): - assert kind in ["loc", "getitem", "iloc", None] - - if kind == "iloc": - self._validate_indexer("positional", key, "iloc") - + def _convert_scalar_indexer(self, key, kind: str): + assert kind in ["loc", "getitem"] + # no-op for non-iloc return key @Appender(Index._convert_slice_indexer.__doc__) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index f55a54a54d0d7..bf42cf0330ef0 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2033,7 +2033,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): return labels._convert_slice_indexer(key, kind="iloc") elif is_float(key): - return labels._convert_scalar_indexer(key, kind="iloc") + labels._validate_indexer("positional", key, "iloc") + return key self._validate_key(key, axis) return key