Skip to content

Commit 8849f18

Browse files
topper-123Terji Petersen
and
Terji Petersen
authored
DEPR: remove Index._is backward_compat_public_numeric_index (#50052)
* DEPR: remove Index._is_backward_compat_public_numeric_index * remove comments Co-authored-by: Terji Petersen <[email protected]>
1 parent cc1a806 commit 8849f18

File tree

7 files changed

+12
-112
lines changed

7 files changed

+12
-112
lines changed

pandas/core/indexes/base.py

+1-53
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
ensure_platform_int,
9191
is_bool_dtype,
9292
is_categorical_dtype,
93-
is_complex_dtype,
9493
is_dtype_equal,
9594
is_ea_or_datetimelike_dtype,
9695
is_extension_array_dtype,
@@ -107,7 +106,6 @@
107106
is_scalar,
108107
is_signed_integer_dtype,
109108
is_string_dtype,
110-
is_unsigned_integer_dtype,
111109
needs_i8_conversion,
112110
pandas_dtype,
113111
validate_all_hashable,
@@ -125,7 +123,6 @@
125123
ABCDatetimeIndex,
126124
ABCMultiIndex,
127125
ABCPeriodIndex,
128-
ABCRangeIndex,
129126
ABCSeries,
130127
ABCTimedeltaIndex,
131128
)
@@ -392,11 +389,6 @@ def _outer_indexer(
392389
_attributes: list[str] = ["name"]
393390
_can_hold_strings: bool = True
394391

395-
# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
396-
# UInt64Index or RangeIndex. Needed for backwards compat. Remove this attribute and
397-
# associated code in pandas 2.0.
398-
_is_backward_compat_public_numeric_index: bool = False
399-
400392
@property
401393
def _engine_type(
402394
self,
@@ -446,13 +438,6 @@ def __new__(
446438
elif is_ea_or_datetimelike_dtype(data_dtype):
447439
pass
448440

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

458443
if isinstance(data, ABCMultiIndex):
@@ -981,34 +966,6 @@ def astype(self, dtype, copy: bool = True):
981966
new_values = astype_array(values, dtype=dtype, copy=copy)
982967

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

1014971
_index_shared_docs[
@@ -5089,10 +5046,6 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
50895046

50905047
result = concat_compat(to_concat_vals)
50915048

5092-
is_numeric = result.dtype.kind in ["i", "u", "f"]
5093-
if self._is_backward_compat_public_numeric_index and is_numeric:
5094-
return type(self)._simple_new(result, name=name)
5095-
50965049
return Index._with_infer(result, name=name)
50975050

50985051
def putmask(self, mask, value) -> Index:
@@ -6490,12 +6443,7 @@ def insert(self, loc: int, item) -> Index:
64906443
loc = loc if loc >= 0 else loc - 1
64916444
new_values[loc] = item
64926445

6493-
if self._typ == "numericindex":
6494-
# Use self._constructor instead of Index to retain NumericIndex GH#43921
6495-
# TODO(2.0) can use Index instead of self._constructor
6496-
return self._constructor(new_values, name=self.name)
6497-
else:
6498-
return Index._with_infer(new_values, name=self.name)
6446+
return Index._with_infer(new_values, name=self.name)
64996447

65006448
def drop(
65016449
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
@@ -415,7 +409,6 @@ class Float64Index(NumericIndex):
415409
_typ = "float64index"
416410
_default_dtype = np.dtype(np.float64)
417411
_dtype_validation_metadata = (is_float_dtype, "float")
418-
_is_backward_compat_public_numeric_index: bool = False
419412

420413
@property
421414
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
@@ -624,14 +624,10 @@ def test_map_dictlike(self, mapper, simple_index):
624624

625625
# empty mappable
626626
dtype = None
627-
if idx._is_backward_compat_public_numeric_index:
628-
new_index_cls = NumericIndex
629-
if idx.dtype.kind == "f":
630-
dtype = idx.dtype
631-
else:
632-
new_index_cls = Float64Index
627+
if idx.dtype.kind == "f":
628+
dtype = idx.dtype
633629

634-
expected = new_index_cls([np.nan] * len(idx), dtype=dtype)
630+
expected = Index([np.nan] * len(idx), dtype=dtype)
635631
result = idx.map(mapper(expected, idx))
636632
tm.assert_index_equal(result, expected)
637633

@@ -896,13 +892,9 @@ def test_insert_na(self, nulls_fixture, simple_index):
896892
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
897893
else:
898894
expected = Index([index[0], np.nan] + list(index[1:]))
899-
900-
if index._is_backward_compat_public_numeric_index:
901-
# GH#43921 we preserve NumericIndex
902-
if index.dtype.kind == "f":
903-
expected = NumericIndex(expected, dtype=index.dtype)
904-
else:
905-
expected = NumericIndex(expected)
895+
# GH#43921 we preserve float dtype
896+
if index.dtype.kind == "f":
897+
expected = Index(expected, dtype=index.dtype)
906898

907899
result = index.insert(1, na_val)
908900
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)