diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 50f0597772cd9..b6bdec2083cd3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3983,6 +3983,13 @@ def _convert_slice_indexer(self, key: slice, kind: str_t): # potentially cast the bounds to integers start, stop, step = key.start, key.stop, key.step + # TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able + # to simplify this. + if isinstance(self.dtype, np.dtype) and is_float_dtype(self.dtype): + # We always treat __getitem__ slicing as label-based + # translate to locations + return self.slice_indexer(start, stop, step) + # figure out if this is a positional indexer def is_int(v): return v is None or is_integer(v) @@ -6284,11 +6291,9 @@ def _maybe_cast_slice_bound(self, label, side: str_t): """ # We are a plain index here (sub-class override this method if they - # wish to have special treatment for floats/ints, e.g. NumericIndex and - # datetimelike Indexes - # Special case numeric EA Indexes, since they are not handled by NumericIndex + # wish to have special treatment for floats/ints, e.g. datetimelike Indexes - if is_extension_array_dtype(self.dtype) and is_numeric_dtype(self.dtype): + if is_numeric_dtype(self.dtype): return self._maybe_cast_indexer(label) # reject them, if index does not contain label diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 4d5e527728567..533ea56d8a7e3 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -12,7 +12,6 @@ from pandas.core.dtypes.common import ( is_dtype_equal, - is_float_dtype, is_integer_dtype, is_numeric_dtype, is_scalar, @@ -170,24 +169,6 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None: def _should_fallback_to_positional(self) -> bool: return False - @doc(Index._convert_slice_indexer) - def _convert_slice_indexer(self, key: slice, kind: str): - # TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able - # to simplify this. - if is_float_dtype(self.dtype): - assert kind in ["loc", "getitem"] - - # We always treat __getitem__ slicing as label-based - # translate to locations - return self.slice_indexer(key.start, key.stop, key.step) - - return super()._convert_slice_indexer(key, kind=kind) - - @doc(Index._maybe_cast_slice_bound) - def _maybe_cast_slice_bound(self, label, side: str): - # we will try to coerce to integers - return self._maybe_cast_indexer(label) - # ---------------------------------------------------------------- @classmethod