Skip to content

Commit f06c96a

Browse files
authored
DEPR: Remove various uses of NumericIndex (#51127)
1 parent ac5bc67 commit f06c96a

File tree

10 files changed

+47
-58
lines changed

10 files changed

+47
-58
lines changed

pandas/core/indexes/datetimes.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
if TYPE_CHECKING:
6363
from pandas.core.api import (
6464
DataFrame,
65-
NumericIndex,
6665
PeriodIndex,
6766
)
6867

@@ -283,11 +282,9 @@ def to_period(self, freq=None) -> PeriodIndex:
283282
return PeriodIndex._simple_new(arr, name=self.name)
284283

285284
@doc(DatetimeArray.to_julian_date)
286-
def to_julian_date(self) -> NumericIndex:
287-
from pandas.core.indexes.api import NumericIndex
288-
285+
def to_julian_date(self) -> Index:
289286
arr = self._data.to_julian_date()
290-
return NumericIndex._simple_new(arr, name=self.name)
287+
return Index._simple_new(arr, name=self.name)
291288

292289
@doc(DatetimeArray.isocalendar)
293290
def isocalendar(self) -> DataFrame:

pandas/tests/dtypes/test_missing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
date_range,
4242
)
4343
import pandas._testing as tm
44-
from pandas.core.api import NumericIndex
4544

4645
fix_now = pd.Timestamp("2021-01-01")
4746
fix_utcnow = pd.Timestamp("2021-01-01", tz="UTC")
@@ -406,10 +405,10 @@ def test_array_equivalent(dtype_equal):
406405
np.array(["a", "b", "c", "d"]), np.array(["e", "e"]), dtype_equal=dtype_equal
407406
)
408407
assert array_equivalent(
409-
NumericIndex([0, np.nan]), NumericIndex([0, np.nan]), dtype_equal=dtype_equal
408+
Index([0, np.nan]), Index([0, np.nan]), dtype_equal=dtype_equal
410409
)
411410
assert not array_equivalent(
412-
NumericIndex([0, np.nan]), NumericIndex([1, np.nan]), dtype_equal=dtype_equal
411+
Index([0, np.nan]), Index([1, np.nan]), dtype_equal=dtype_equal
413412
)
414413
assert array_equivalent(
415414
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan]), dtype_equal=dtype_equal

pandas/tests/groupby/test_pipe.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Index,
77
)
88
import pandas._testing as tm
9-
from pandas.core.api import NumericIndex
109

1110

1211
def test_pipe():
@@ -76,6 +75,6 @@ def h(df, arg3):
7675
ser = pd.Series([1, 1, 2, 2, 3, 3])
7776
result = ser.groupby(ser).pipe(lambda grp: grp.sum() * grp.count())
7877

79-
expected = pd.Series([4, 8, 12], index=NumericIndex([1, 2, 3], dtype=np.int64))
78+
expected = pd.Series([4, 8, 12], index=Index([1, 2, 3], dtype=np.int64))
8079

8180
tm.assert_series_equal(result, expected)

pandas/tests/indexes/common.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
isna,
2929
)
3030
import pandas._testing as tm
31-
from pandas.core.api import NumericIndex
3231
from pandas.core.arrays import BaseMaskedArray
3332

3433

@@ -317,9 +316,7 @@ def test_numpy_argsort(self, index):
317316
def test_repeat(self, simple_index):
318317
rep = 2
319318
idx = simple_index.copy()
320-
new_index_cls = (
321-
NumericIndex if isinstance(idx, RangeIndex) else idx._constructor
322-
)
319+
new_index_cls = idx._constructor
323320
expected = new_index_cls(idx.values.repeat(rep), name=idx.name)
324321
tm.assert_index_equal(idx.repeat(rep), expected)
325322

@@ -523,7 +520,7 @@ def test_fillna(self, index):
523520
elif index.dtype == bool:
524521
# can't hold NAs
525522
return
526-
elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype):
523+
elif isinstance(index, Index) and is_integer_dtype(index.dtype):
527524
return
528525
elif isinstance(index, MultiIndex):
529526
idx = index.copy(deep=True)
@@ -592,7 +589,7 @@ def test_map(self, simple_index):
592589
idx = simple_index
593590

594591
result = idx.map(lambda x: x)
595-
# RangeIndex are equivalent to the similar NumericIndex with int64 dtype
592+
# RangeIndex are equivalent to the similar Index with int64 dtype
596593
tm.assert_index_equal(result, idx, exact="equiv")
597594

598595
@pytest.mark.parametrize(
@@ -615,7 +612,7 @@ def test_map_dictlike(self, mapper, simple_index):
615612
identity = mapper(idx.values, idx)
616613

617614
result = idx.map(identity)
618-
# RangeIndex are equivalent to the similar NumericIndex with int64 dtype
615+
# RangeIndex are equivalent to the similar Index with int64 dtype
619616
tm.assert_index_equal(result, idx, exact="equiv")
620617

621618
# empty mappable
@@ -753,7 +750,7 @@ def test_index_groupby(self, simple_index):
753750
tm.assert_dict_equal(idx.groupby(to_groupby), expected)
754751

755752
def test_append_preserves_dtype(self, simple_index):
756-
# In particular NumericIndex with dtype float32
753+
# In particular Index with dtype float32
757754
index = simple_index
758755
N = len(index)
759756

@@ -926,19 +923,19 @@ def test_arithmetic_explicit_conversions(self):
926923

927924
# float conversions
928925
arr = np.arange(5, dtype="int64") * 3.2
929-
expected = NumericIndex(arr, dtype=np.float64)
926+
expected = Index(arr, dtype=np.float64)
930927
fidx = idx * 3.2
931928
tm.assert_index_equal(fidx, expected)
932929
fidx = 3.2 * idx
933930
tm.assert_index_equal(fidx, expected)
934931

935932
# interops with numpy arrays
936-
expected = NumericIndex(arr, dtype=np.float64)
933+
expected = Index(arr, dtype=np.float64)
937934
a = np.zeros(5, dtype="float64")
938935
result = fidx - a
939936
tm.assert_index_equal(result, expected)
940937

941-
expected = NumericIndex(-arr, dtype=np.float64)
938+
expected = Index(-arr, dtype=np.float64)
942939
a = np.zeros(5, dtype="float64")
943940
result = a - fidx
944941
tm.assert_index_equal(result, expected)

pandas/tests/indexes/interval/test_constructors.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
timedelta_range,
1919
)
2020
import pandas._testing as tm
21-
from pandas.core.api import NumericIndex
2221
from pandas.core.arrays import IntervalArray
2322
import pandas.core.common as com
2423

@@ -39,9 +38,9 @@ class ConstructorTests:
3938
params=[
4039
([3, 14, 15, 92, 653], np.int64),
4140
(np.arange(10, dtype="int64"), np.int64),
42-
(NumericIndex(np.arange(-10, 11, dtype=np.int64)), np.int64),
43-
(NumericIndex(np.arange(10, 31, dtype=np.uint64)), np.uint64),
44-
(NumericIndex(np.arange(20, 30, 0.5), dtype=np.float64), np.float64),
41+
(Index(np.arange(-10, 11, dtype=np.int64)), np.int64),
42+
(Index(np.arange(10, 31, dtype=np.uint64)), np.uint64),
43+
(Index(np.arange(20, 30, 0.5), dtype=np.float64), np.float64),
4544
(date_range("20180101", periods=10), "<M8[ns]"),
4645
(
4746
date_range("20180101", periods=10, tz="US/Eastern"),
@@ -69,10 +68,10 @@ def test_constructor(self, constructor, breaks_and_expected_subtype, closed, nam
6968
@pytest.mark.parametrize(
7069
"breaks, subtype",
7170
[
72-
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "float64"),
73-
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "datetime64[ns]"),
74-
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "timedelta64[ns]"),
75-
(NumericIndex([0, 1, 2, 3, 4], dtype=np.float64), "int64"),
71+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "float64"),
72+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "datetime64[ns]"),
73+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "timedelta64[ns]"),
74+
(Index([0, 1, 2, 3, 4], dtype=np.float64), "int64"),
7675
(date_range("2017-01-01", periods=5), "int64"),
7776
(timedelta_range("1 day", periods=5), "int64"),
7877
],
@@ -91,9 +90,9 @@ def test_constructor_dtype(self, constructor, breaks, subtype):
9190
@pytest.mark.parametrize(
9291
"breaks",
9392
[
94-
NumericIndex([0, 1, 2, 3, 4], dtype=np.int64),
95-
NumericIndex([0, 1, 2, 3, 4], dtype=np.uint64),
96-
NumericIndex([0, 1, 2, 3, 4], dtype=np.float64),
93+
Index([0, 1, 2, 3, 4], dtype=np.int64),
94+
Index([0, 1, 2, 3, 4], dtype=np.uint64),
95+
Index([0, 1, 2, 3, 4], dtype=np.float64),
9796
date_range("2017-01-01", periods=5),
9897
timedelta_range("1 day", periods=5),
9998
],
@@ -250,8 +249,8 @@ def test_mixed_float_int(self, left_subtype, right_subtype):
250249
right = np.arange(1, 10, dtype=right_subtype)
251250
result = IntervalIndex.from_arrays(left, right)
252251

253-
expected_left = NumericIndex(left, dtype=np.float64)
254-
expected_right = NumericIndex(right, dtype=np.float64)
252+
expected_left = Index(left, dtype=np.float64)
253+
expected_right = Index(right, dtype=np.float64)
255254
expected_subtype = np.float64
256255

257256
tm.assert_index_equal(result.left, expected_left)

pandas/tests/indexes/test_base.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
period_range,
3939
)
4040
import pandas._testing as tm
41-
from pandas.core.api import NumericIndex
4241
from pandas.core.indexes.api import (
4342
Index,
4443
MultiIndex,
@@ -195,14 +194,14 @@ def test_constructor_from_frame_series_freq(self):
195194
def test_constructor_int_dtype_nan(self):
196195
# see gh-15187
197196
data = [np.nan]
198-
expected = NumericIndex(data, dtype=np.float64)
197+
expected = Index(data, dtype=np.float64)
199198
result = Index(data, dtype="float")
200199
tm.assert_index_equal(result, expected)
201200

202201
@pytest.mark.parametrize(
203202
"klass,dtype,na_val",
204203
[
205-
(NumericIndex, np.float64, np.nan),
204+
(Index, np.float64, np.nan),
206205
(DatetimeIndex, "datetime64[ns]", pd.NaT),
207206
],
208207
)
@@ -868,7 +867,8 @@ def test_isin_nan_common_object(self, nulls_fixture, nulls_fixture2):
868867
np.array([False, False]),
869868
)
870869

871-
def test_isin_nan_common_float64(self, nulls_fixture):
870+
def test_isin_nan_common_float64(self, nulls_fixture, float_numpy_dtype):
871+
dtype = float_numpy_dtype
872872

873873
if nulls_fixture is pd.NaT or nulls_fixture is pd.NA:
874874
# Check 1) that we cannot construct a float64 Index with this value
@@ -878,13 +878,13 @@ def test_isin_nan_common_float64(self, nulls_fixture):
878878
f"not {repr(type(nulls_fixture).__name__)}"
879879
)
880880
with pytest.raises(TypeError, match=msg):
881-
NumericIndex([1.0, nulls_fixture], dtype=np.float64)
881+
Index([1.0, nulls_fixture], dtype=dtype)
882882

883-
idx = NumericIndex([1.0, np.nan], dtype=np.float64)
883+
idx = Index([1.0, np.nan], dtype=dtype)
884884
assert not idx.isin([nulls_fixture]).any()
885885
return
886886

887-
idx = NumericIndex([1.0, nulls_fixture], dtype=np.float64)
887+
idx = Index([1.0, nulls_fixture], dtype=dtype)
888888
res = idx.isin([np.nan])
889889
tm.assert_numpy_array_equal(res, np.array([False, True]))
890890

@@ -897,7 +897,7 @@ def test_isin_nan_common_float64(self, nulls_fixture):
897897
"index",
898898
[
899899
Index(["qux", "baz", "foo", "bar"]),
900-
NumericIndex([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
900+
Index([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
901901
],
902902
)
903903
def test_isin_level_kwarg(self, level, index):
@@ -1150,7 +1150,7 @@ def test_reindex_doesnt_preserve_type_if_target_is_empty_index_numeric(
11501150
# GH7774
11511151
dtype = any_real_numpy_dtype
11521152
index = Index(list("abc"))
1153-
labels = NumericIndex([], dtype=dtype)
1153+
labels = Index([], dtype=dtype)
11541154
assert index.reindex(labels)[0].dtype == dtype
11551155

11561156
def test_reindex_no_type_preserve_target_empty_mi(self):
@@ -1596,11 +1596,9 @@ def test_validate_1d_input(dtype):
15961596
"klass, extra_kwargs",
15971597
[
15981598
[Index, {}],
1599-
[lambda x: NumericIndex(x, np.int64), {}],
1600-
[lambda x: NumericIndex(x, np.float64), {}],
1599+
*[[lambda x: Index(x, dtype=dtyp), {}] for dtyp in tm.ALL_REAL_NUMPY_DTYPES],
16011600
[DatetimeIndex, {}],
16021601
[TimedeltaIndex, {}],
1603-
[NumericIndex, {}],
16041602
[PeriodIndex, {"freq": "Y"}],
16051603
],
16061604
)

pandas/tests/indexes/test_common.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
)
1919
from pandas.errors import PerformanceWarning
2020

21-
from pandas.core.dtypes.common import is_integer_dtype
21+
from pandas.core.dtypes.common import (
22+
is_integer_dtype,
23+
is_numeric_dtype,
24+
)
2225

2326
import pandas as pd
2427
from pandas import (
@@ -316,7 +319,7 @@ def test_drop_duplicates(self, index_flat, keep):
316319
# make unique index
317320
holder = type(index)
318321
unique_values = list(set(index))
319-
dtype = index.dtype if isinstance(index, NumericIndex) else None
322+
dtype = index.dtype if is_numeric_dtype(index) else None
320323
unique_idx = holder(unique_values, dtype=dtype)
321324

322325
# make duplicated index
@@ -345,7 +348,7 @@ def test_drop_duplicates_no_duplicates(self, index_flat):
345348
else:
346349
holder = type(index)
347350
unique_values = list(set(index))
348-
dtype = index.dtype if isinstance(index, NumericIndex) else None
351+
dtype = index.dtype if is_numeric_dtype(index) else None
349352
unique_idx = holder(unique_values, dtype=dtype)
350353

351354
# check on unique index

pandas/tests/indexes/test_index_new.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
timedelta_range,
3030
)
3131
import pandas._testing as tm
32-
from pandas.core.api import NumericIndex
3332

3433

3534
class TestIndexConstructorInference:
@@ -84,7 +83,7 @@ def test_construction_list_tuples_nan(self, na_value, vtype):
8483
)
8584
def test_constructor_int_dtype_float(self, dtype):
8685
# GH#18400
87-
expected = NumericIndex([0, 1, 2, 3], dtype=dtype)
86+
expected = Index([0, 1, 2, 3], dtype=dtype)
8887
result = Index([0.0, 1.0, 2.0, 3.0], dtype=dtype)
8988
tm.assert_index_equal(result, expected)
9089

@@ -283,7 +282,7 @@ def test_constructor_int_dtype_nan_raises(self, dtype):
283282
)
284283
def test_constructor_dtypes_to_int(self, vals, any_int_numpy_dtype):
285284
dtype = any_int_numpy_dtype
286-
index = NumericIndex(vals, dtype=dtype)
285+
index = Index(vals, dtype=dtype)
287286
assert index.dtype == dtype
288287

289288
@pytest.mark.parametrize(
@@ -298,7 +297,7 @@ def test_constructor_dtypes_to_int(self, vals, any_int_numpy_dtype):
298297
)
299298
def test_constructor_dtypes_to_float(self, vals, float_numpy_dtype):
300299
dtype = float_numpy_dtype
301-
index = NumericIndex(vals, dtype=dtype)
300+
index = Index(vals, dtype=dtype)
302301
assert index.dtype == dtype
303302

304303
@pytest.mark.parametrize(

pandas/tests/indexes/test_indexing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
TimedeltaIndex,
3434
)
3535
import pandas._testing as tm
36-
from pandas.core.api import NumericIndex
3736

3837

3938
class TestTake:
@@ -141,7 +140,7 @@ def test_contains_with_float_index(self, any_real_numpy_dtype):
141140
# GH#22085
142141
dtype = any_real_numpy_dtype
143142
data = [0, 1, 2, 3] if not is_float_dtype(dtype) else [0.1, 1.1, 2.2, 3.3]
144-
index = NumericIndex(data, dtype=dtype)
143+
index = Index(data, dtype=dtype)
145144

146145
if not is_float_dtype(index.dtype):
147146
assert 1.1 not in index

pandas/tests/indexing/test_floats.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Series,
99
)
1010
import pandas._testing as tm
11-
from pandas.core.api import NumericIndex
1211

1312

1413
def gen_obj(klass, index):
@@ -261,9 +260,9 @@ def test_slice_integer(self):
261260
# oob indicates if we are out of bounds
262261
# of positional indexing
263262
for index, oob in [
264-
(NumericIndex(np.arange(5, dtype=np.int64)), False),
263+
(Index(np.arange(5, dtype=np.int64)), False),
265264
(RangeIndex(5), False),
266-
(NumericIndex(np.arange(5, dtype=np.int64) + 10), True),
265+
(Index(np.arange(5, dtype=np.int64) + 10), True),
267266
]:
268267

269268
# s is an in-range index

0 commit comments

Comments
 (0)