From 27e65d49f69aa20387971c16e51f598a1e201f27 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Thu, 26 Jan 2023 06:32:44 +0000 Subject: [PATCH 1/3] DEPR: move NumericIndex._convert_slice_indexer to Index --- pandas/core/indexes/base.py | 7 +++++++ pandas/core/indexes/numeric.py | 14 -------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 50f0597772cd9..6e5706f0638cc 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 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) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 4d5e527728567..8e8a39f5a8bdd 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,19 +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 From 38af8b5a920caccf595787d5dafa01fd758adb4f Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Thu, 26 Jan 2023 06:34:14 +0000 Subject: [PATCH 2/3] DEPR: move NumericIndex._maybe_cast_slice_bound to Index --- pandas/core/indexes/base.py | 6 ++---- pandas/core/indexes/numeric.py | 5 ----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6e5706f0638cc..bfa4c52fa6e34 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6291,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 8e8a39f5a8bdd..533ea56d8a7e3 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -169,11 +169,6 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None: def _should_fallback_to_positional(self) -> bool: return False - @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 From b63a3386543830a8a3fe561633a73ebe9f62f379 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Thu, 26 Jan 2023 20:34:44 +0000 Subject: [PATCH 3/3] fix if ststement --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index bfa4c52fa6e34..b6bdec2083cd3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3985,7 +3985,7 @@ def _convert_slice_indexer(self, key: slice, kind: str_t): # TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able # to simplify this. - if is_float_dtype(self.dtype): + 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)