Skip to content

Commit c9f6171

Browse files
Terji Petersentopper-123
Terji Petersen
authored andcommitted
DEPR: remove Index._is_backward_compat_public_numeric_index
1 parent 4c337f6 commit c9f6171

File tree

7 files changed

+12
-106
lines changed

7 files changed

+12
-106
lines changed

pandas/core/indexes/base.py

+1-47
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
is_scalar,
107107
is_signed_integer_dtype,
108108
is_string_dtype,
109-
is_unsigned_integer_dtype,
110109
needs_i8_conversion,
111110
pandas_dtype,
112111
validate_all_hashable,
@@ -394,7 +393,6 @@ def _outer_indexer(
394393
# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
395394
# UInt64Index or RangeIndex. Needed for backwards compat. Remove this attribute and
396395
# associated code in pandas 2.0.
397-
_is_backward_compat_public_numeric_index: bool = False
398396

399397
@property
400398
def _engine_type(
@@ -445,13 +443,6 @@ def __new__(
445443
elif is_ea_or_datetimelike_dtype(data_dtype):
446444
pass
447445

448-
# index-like
449-
elif (
450-
isinstance(data, Index)
451-
and data._is_backward_compat_public_numeric_index
452-
and dtype is None
453-
):
454-
return data._constructor(data, name=name, copy=copy)
455446
elif isinstance(data, (np.ndarray, Index, ABCSeries)):
456447

457448
if isinstance(data, ABCMultiIndex):
@@ -980,34 +971,6 @@ def astype(self, dtype, copy: bool = True):
980971
new_values = astype_array(values, dtype=dtype, copy=copy)
981972

982973
# pass copy=False because any copying will be done in the astype above
983-
if not self._is_backward_compat_public_numeric_index and not isinstance(
984-
self, ABCRangeIndex
985-
):
986-
# this block is needed so e.g. Int64Index.astype("int32") returns
987-
# Int64Index and not a NumericIndex with dtype int32.
988-
# When Int64Index etc. are removed from the code base, removed this also.
989-
if (
990-
isinstance(dtype, np.dtype)
991-
and is_numeric_dtype(dtype)
992-
and not is_complex_dtype(dtype)
993-
):
994-
from pandas.core.api import (
995-
Float64Index,
996-
Int64Index,
997-
UInt64Index,
998-
)
999-
1000-
klass: type[Index]
1001-
if is_signed_integer_dtype(dtype):
1002-
klass = Int64Index
1003-
elif is_unsigned_integer_dtype(dtype):
1004-
klass = UInt64Index
1005-
elif is_float_dtype(dtype):
1006-
klass = Float64Index
1007-
else:
1008-
klass = Index
1009-
return klass(new_values, name=self.name, dtype=dtype, copy=False)
1010-
1011974
return Index(new_values, name=self.name, dtype=new_values.dtype, copy=False)
1012975

1013976
_index_shared_docs[
@@ -5031,10 +4994,6 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
50314994

50324995
result = concat_compat(to_concat_vals)
50334996

5034-
is_numeric = result.dtype.kind in ["i", "u", "f"]
5035-
if self._is_backward_compat_public_numeric_index and is_numeric:
5036-
return type(self)._simple_new(result, name=name)
5037-
50384997
return Index._with_infer(result, name=name)
50394998

50404999
def putmask(self, mask, value) -> Index:
@@ -6432,12 +6391,7 @@ def insert(self, loc: int, item) -> Index:
64326391
loc = loc if loc >= 0 else loc - 1
64336392
new_values[loc] = item
64346393

6435-
if self._typ == "numericindex":
6436-
# Use self._constructor instead of Index to retain NumericIndex GH#43921
6437-
# TODO(2.0) can use Index instead of self._constructor
6438-
return self._constructor(new_values, name=self.name)
6439-
else:
6440-
return Index._with_infer(new_values, name=self.name)
6394+
return Index._with_infer(new_values, name=self.name)
64416395

64426396
def drop(
64436397
self,

pandas/core/indexes/category.py

-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from pandas.core.dtypes.common import (
2222
is_categorical_dtype,
2323
is_scalar,
24-
pandas_dtype,
2524
)
2625
from pandas.core.dtypes.missing import (
2726
is_valid_na_for_dtype,
@@ -274,30 +273,6 @@ def _is_dtype_compat(self, other) -> Categorical:
274273

275274
return other
276275

277-
@doc(Index.astype)
278-
def astype(self, dtype: Dtype, copy: bool = True) -> Index:
279-
from pandas.core.api import NumericIndex
280-
281-
dtype = pandas_dtype(dtype)
282-
283-
categories = self.categories
284-
# the super method always returns Int64Index, UInt64Index and Float64Index
285-
# but if the categories are a NumericIndex with dtype float32, we want to
286-
# return an index with the same dtype as self.categories.
287-
if categories._is_backward_compat_public_numeric_index:
288-
assert isinstance(categories, NumericIndex) # mypy complaint fix
289-
try:
290-
categories._validate_dtype(dtype)
291-
except ValueError:
292-
pass
293-
else:
294-
new_values = self._data.astype(dtype, copy=copy)
295-
# pass copy=False because any copying has been done in the
296-
# _data.astype call above
297-
return categories._constructor(new_values, name=self.name, copy=False)
298-
299-
return super().astype(dtype, copy=copy)
300-
301276
def equals(self, other: object) -> bool:
302277
"""
303278
Determine if two CategoricalIndex objects contain the same elements.

pandas/core/indexes/numeric.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class NumericIndex(Index):
8787
"numeric type",
8888
)
8989
_can_hold_strings = False
90-
_is_backward_compat_public_numeric_index: bool = True
9190

9291
_engine_types: dict[np.dtype, type[libindex.IndexEngine]] = {
9392
np.dtype(np.int8): libindex.Int8Engine,
@@ -214,12 +213,7 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
214213
# float16 not supported (no indexing engine)
215214
raise NotImplementedError("float16 indexes are not supported")
216215

217-
if cls._is_backward_compat_public_numeric_index:
218-
# dtype for NumericIndex
219-
return dtype
220-
else:
221-
# dtype for Int64Index, UInt64Index etc. Needed for backwards compat.
222-
return cls._default_dtype
216+
return dtype
223217

224218
# ----------------------------------------------------------------
225219
# Indexing Methods
@@ -417,7 +411,6 @@ class Float64Index(NumericIndex):
417411
_typ = "float64index"
418412
_default_dtype = np.dtype(np.float64)
419413
_dtype_validation_metadata = (is_float_dtype, "float")
420-
_is_backward_compat_public_numeric_index: bool = False
421414

422415
@property
423416
def _engine_type(self) -> type[libindex.Float64Engine]:

pandas/core/indexes/range.py

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class RangeIndex(NumericIndex):
103103
_typ = "rangeindex"
104104
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
105105
_range: range
106-
_is_backward_compat_public_numeric_index: bool = False
107106

108107
@property
109108
def _engine_type(self) -> type[libindex.Int64Engine]:

pandas/tests/base/test_unique.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pandas as pd
77
import pandas._testing as tm
8-
from pandas.core.api import NumericIndex
98
from pandas.tests.base.common import allow_na_ops
109

1110

@@ -20,9 +19,6 @@ def test_unique(index_or_series_obj):
2019
expected = pd.MultiIndex.from_tuples(unique_values)
2120
expected.names = obj.names
2221
tm.assert_index_equal(result, expected, exact=True)
23-
elif isinstance(obj, pd.Index) and obj._is_backward_compat_public_numeric_index:
24-
expected = NumericIndex(unique_values, dtype=obj.dtype)
25-
tm.assert_index_equal(result, expected, exact=True)
2622
elif isinstance(obj, pd.Index):
2723
expected = pd.Index(unique_values, dtype=obj.dtype)
2824
if is_datetime64tz_dtype(obj.dtype):
@@ -58,10 +54,7 @@ def test_unique_null(null_obj, index_or_series_obj):
5854
unique_values_not_null = [val for val in unique_values_raw if not pd.isnull(val)]
5955
unique_values = [null_obj] + unique_values_not_null
6056

61-
if isinstance(obj, pd.Index) and obj._is_backward_compat_public_numeric_index:
62-
expected = NumericIndex(unique_values, dtype=obj.dtype)
63-
tm.assert_index_equal(result, expected, exact=True)
64-
elif isinstance(obj, pd.Index):
57+
if isinstance(obj, pd.Index):
6558
expected = pd.Index(unique_values, dtype=obj.dtype)
6659
if is_datetime64tz_dtype(obj.dtype):
6760
result = result.normalize()

pandas/tests/indexes/common.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -622,14 +622,10 @@ def test_map_dictlike(self, mapper, simple_index):
622622

623623
# empty mappable
624624
dtype = None
625-
if idx._is_backward_compat_public_numeric_index:
626-
new_index_cls = NumericIndex
627-
if idx.dtype.kind == "f":
628-
dtype = idx.dtype
629-
else:
630-
new_index_cls = Float64Index
625+
if idx.dtype.kind == "f":
626+
dtype = idx.dtype
631627

632-
expected = new_index_cls([np.nan] * len(idx), dtype=dtype)
628+
expected = Index([np.nan] * len(idx), dtype=dtype)
633629
result = idx.map(mapper(expected, idx))
634630
tm.assert_index_equal(result, expected)
635631

@@ -860,13 +856,9 @@ def test_insert_na(self, nulls_fixture, simple_index):
860856
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
861857
else:
862858
expected = Index([index[0], np.nan] + list(index[1:]))
863-
864-
if index._is_backward_compat_public_numeric_index:
865-
# GH#43921 we preserve NumericIndex
866-
if index.dtype.kind == "f":
867-
expected = NumericIndex(expected, dtype=index.dtype)
868-
else:
869-
expected = NumericIndex(expected)
859+
# GH#43921 we preserve float dtype
860+
if index.dtype.kind == "f":
861+
expected = Index(expected, dtype=index.dtype)
870862

871863
result = index.insert(1, na_val)
872864
tm.assert_index_equal(result, expected, exact=True)

pandas/tests/indexes/test_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
)
1919
from pandas.util._test_decorators import async_mark
2020

21+
from pandas.core.dtypes.common import is_numeric_dtype
22+
2123
import pandas as pd
2224
from pandas import (
2325
CategoricalIndex,
@@ -592,13 +594,11 @@ def test_map_dictlike(self, index, mapper, request):
592594
if index.empty:
593595
# to match proper result coercion for uints
594596
expected = Index([])
595-
elif index._is_backward_compat_public_numeric_index:
597+
elif is_numeric_dtype(index.dtype):
596598
expected = index._constructor(rng, dtype=index.dtype)
597599
elif type(index) is Index and index.dtype != object:
598600
# i.e. EA-backed, for now just Nullable
599601
expected = Index(rng, dtype=index.dtype)
600-
elif index.dtype.kind == "u":
601-
expected = Index(rng, dtype=index.dtype)
602602
else:
603603
expected = Index(rng)
604604

0 commit comments

Comments
 (0)