Skip to content

DEPR: Remove various uses of NumericIndex #51127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
if TYPE_CHECKING:
from pandas.core.api import (
DataFrame,
NumericIndex,
PeriodIndex,
)

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

@doc(DatetimeArray.to_julian_date)
def to_julian_date(self) -> NumericIndex:
from pandas.core.indexes.api import NumericIndex

def to_julian_date(self) -> Index:
arr = self._data.to_julian_date()
return NumericIndex._simple_new(arr, name=self.name)
return Index._simple_new(arr, name=self.name)

@doc(DatetimeArray.isocalendar)
def isocalendar(self) -> DataFrame:
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
date_range,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex

fix_now = pd.Timestamp("2021-01-01")
fix_utcnow = pd.Timestamp("2021-01-01", tz="UTC")
Expand Down Expand Up @@ -406,10 +405,10 @@ def test_array_equivalent(dtype_equal):
np.array(["a", "b", "c", "d"]), np.array(["e", "e"]), dtype_equal=dtype_equal
)
assert array_equivalent(
NumericIndex([0, np.nan]), NumericIndex([0, np.nan]), dtype_equal=dtype_equal
Index([0, np.nan]), Index([0, np.nan]), dtype_equal=dtype_equal
)
assert not array_equivalent(
NumericIndex([0, np.nan]), NumericIndex([1, np.nan]), dtype_equal=dtype_equal
Index([0, np.nan]), Index([1, np.nan]), dtype_equal=dtype_equal
)
assert array_equivalent(
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan]), dtype_equal=dtype_equal
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/groupby/test_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Index,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex


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

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

tm.assert_series_equal(result, expected)
19 changes: 8 additions & 11 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
isna,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex
from pandas.core.arrays import BaseMaskedArray


Expand Down Expand Up @@ -317,9 +316,7 @@ def test_numpy_argsort(self, index):
def test_repeat(self, simple_index):
rep = 2
idx = simple_index.copy()
new_index_cls = (
NumericIndex if isinstance(idx, RangeIndex) else idx._constructor
)
new_index_cls = idx._constructor
expected = new_index_cls(idx.values.repeat(rep), name=idx.name)
tm.assert_index_equal(idx.repeat(rep), expected)

Expand Down Expand Up @@ -523,7 +520,7 @@ def test_fillna(self, index):
elif index.dtype == bool:
# can't hold NAs
return
elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype):
elif isinstance(index, Index) and is_integer_dtype(index.dtype):
return
elif isinstance(index, MultiIndex):
idx = index.copy(deep=True)
Expand Down Expand Up @@ -592,7 +589,7 @@ def test_map(self, simple_index):
idx = simple_index

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

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

result = idx.map(identity)
# RangeIndex are equivalent to the similar NumericIndex with int64 dtype
# RangeIndex are equivalent to the similar Index with int64 dtype
tm.assert_index_equal(result, idx, exact="equiv")

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

def test_append_preserves_dtype(self, simple_index):
# In particular NumericIndex with dtype float32
# In particular Index with dtype float32
index = simple_index
N = len(index)

Expand Down Expand Up @@ -917,19 +914,19 @@ def test_arithmetic_explicit_conversions(self):

# float conversions
arr = np.arange(5, dtype="int64") * 3.2
expected = NumericIndex(arr, dtype=np.float64)
expected = Index(arr, dtype=np.float64)
fidx = idx * 3.2
tm.assert_index_equal(fidx, expected)
fidx = 3.2 * idx
tm.assert_index_equal(fidx, expected)

# interops with numpy arrays
expected = NumericIndex(arr, dtype=np.float64)
expected = Index(arr, dtype=np.float64)
a = np.zeros(5, dtype="float64")
result = fidx - a
tm.assert_index_equal(result, expected)

expected = NumericIndex(-arr, dtype=np.float64)
expected = Index(-arr, dtype=np.float64)
a = np.zeros(5, dtype="float64")
result = a - fidx
tm.assert_index_equal(result, expected)
Expand Down
25 changes: 12 additions & 13 deletions pandas/tests/indexes/interval/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex
from pandas.core.arrays import IntervalArray
import pandas.core.common as com

Expand All @@ -39,9 +38,9 @@ class ConstructorTests:
params=[
([3, 14, 15, 92, 653], np.int64),
(np.arange(10, dtype="int64"), np.int64),
(NumericIndex(np.arange(-10, 11, dtype=np.int64)), np.int64),
(NumericIndex(np.arange(10, 31, dtype=np.uint64)), np.uint64),
(NumericIndex(np.arange(20, 30, 0.5), dtype=np.float64), np.float64),
(Index(np.arange(-10, 11, dtype=np.int64)), np.int64),
(Index(np.arange(10, 31, dtype=np.uint64)), np.uint64),
(Index(np.arange(20, 30, 0.5), dtype=np.float64), np.float64),
(date_range("20180101", periods=10), "<M8[ns]"),
(
date_range("20180101", periods=10, tz="US/Eastern"),
Expand Down Expand Up @@ -69,10 +68,10 @@ def test_constructor(self, constructor, breaks_and_expected_subtype, closed, nam
@pytest.mark.parametrize(
"breaks, subtype",
[
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "float64"),
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "datetime64[ns]"),
(NumericIndex([0, 1, 2, 3, 4], dtype=np.int64), "timedelta64[ns]"),
(NumericIndex([0, 1, 2, 3, 4], dtype=np.float64), "int64"),
(Index([0, 1, 2, 3, 4], dtype=np.int64), "float64"),
(Index([0, 1, 2, 3, 4], dtype=np.int64), "datetime64[ns]"),
(Index([0, 1, 2, 3, 4], dtype=np.int64), "timedelta64[ns]"),
(Index([0, 1, 2, 3, 4], dtype=np.float64), "int64"),
(date_range("2017-01-01", periods=5), "int64"),
(timedelta_range("1 day", periods=5), "int64"),
],
Expand All @@ -91,9 +90,9 @@ def test_constructor_dtype(self, constructor, breaks, subtype):
@pytest.mark.parametrize(
"breaks",
[
NumericIndex([0, 1, 2, 3, 4], dtype=np.int64),
NumericIndex([0, 1, 2, 3, 4], dtype=np.uint64),
NumericIndex([0, 1, 2, 3, 4], dtype=np.float64),
Index([0, 1, 2, 3, 4], dtype=np.int64),
Index([0, 1, 2, 3, 4], dtype=np.uint64),
Index([0, 1, 2, 3, 4], dtype=np.float64),
date_range("2017-01-01", periods=5),
timedelta_range("1 day", periods=5),
],
Expand Down Expand Up @@ -250,8 +249,8 @@ def test_mixed_float_int(self, left_subtype, right_subtype):
right = np.arange(1, 10, dtype=right_subtype)
result = IntervalIndex.from_arrays(left, right)

expected_left = NumericIndex(left, dtype=np.float64)
expected_right = NumericIndex(right, dtype=np.float64)
expected_left = Index(left, dtype=np.float64)
expected_right = Index(right, dtype=np.float64)
expected_subtype = np.float64

tm.assert_index_equal(result.left, expected_left)
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
period_range,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex
from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand Down Expand Up @@ -194,14 +193,14 @@ def test_constructor_from_frame_series_freq(self):
def test_constructor_int_dtype_nan(self):
# see gh-15187
data = [np.nan]
expected = NumericIndex(data, dtype=np.float64)
expected = Index(data, dtype=np.float64)
result = Index(data, dtype="float")
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"klass,dtype,na_val",
[
(NumericIndex, np.float64, np.nan),
(Index, np.float64, np.nan),
(DatetimeIndex, "datetime64[ns]", pd.NaT),
],
)
Expand Down Expand Up @@ -867,7 +866,8 @@ def test_isin_nan_common_object(self, nulls_fixture, nulls_fixture2):
np.array([False, False]),
)

def test_isin_nan_common_float64(self, nulls_fixture):
def test_isin_nan_common_float64(self, nulls_fixture, float_numpy_dtype):
dtype = float_numpy_dtype

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

idx = NumericIndex([1.0, np.nan], dtype=np.float64)
idx = Index([1.0, np.nan], dtype=dtype)
assert not idx.isin([nulls_fixture]).any()
return

idx = NumericIndex([1.0, nulls_fixture], dtype=np.float64)
idx = Index([1.0, nulls_fixture], dtype=dtype)
res = idx.isin([np.nan])
tm.assert_numpy_array_equal(res, np.array([False, True]))

Expand All @@ -896,7 +896,7 @@ def test_isin_nan_common_float64(self, nulls_fixture):
"index",
[
Index(["qux", "baz", "foo", "bar"]),
NumericIndex([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
Index([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
],
)
def test_isin_level_kwarg(self, level, index):
Expand Down Expand Up @@ -1149,7 +1149,7 @@ def test_reindex_doesnt_preserve_type_if_target_is_empty_index_numeric(
# GH7774
dtype = any_real_numpy_dtype
index = Index(list("abc"))
labels = NumericIndex([], dtype=dtype)
labels = Index([], dtype=dtype)
assert index.reindex(labels)[0].dtype == dtype

def test_reindex_no_type_preserve_target_empty_mi(self):
Expand Down Expand Up @@ -1595,11 +1595,9 @@ def test_validate_1d_input(dtype):
"klass, extra_kwargs",
[
[Index, {}],
[lambda x: NumericIndex(x, np.int64), {}],
[lambda x: NumericIndex(x, np.float64), {}],
*[[lambda x: Index(x, dtype=dtyp), {}] for dtyp in tm.ALL_REAL_NUMPY_DTYPES],
[DatetimeIndex, {}],
[TimedeltaIndex, {}],
[NumericIndex, {}],
[PeriodIndex, {"freq": "Y"}],
],
)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
)
from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import is_integer_dtype
from pandas.core.dtypes.common import (
is_integer_dtype,
is_numeric_dtype,
)

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -316,7 +319,7 @@ def test_drop_duplicates(self, index_flat, keep):
# make unique index
holder = type(index)
unique_values = list(set(index))
dtype = index.dtype if isinstance(index, NumericIndex) else None
dtype = index.dtype if is_numeric_dtype(index) else None
unique_idx = holder(unique_values, dtype=dtype)

# make duplicated index
Expand Down Expand Up @@ -345,7 +348,7 @@ def test_drop_duplicates_no_duplicates(self, index_flat):
else:
holder = type(index)
unique_values = list(set(index))
dtype = index.dtype if isinstance(index, NumericIndex) else None
dtype = index.dtype if is_numeric_dtype(index) else None
unique_idx = holder(unique_values, dtype=dtype)

# check on unique index
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex


class TestIndexConstructorInference:
Expand Down Expand Up @@ -84,7 +83,7 @@ def test_construction_list_tuples_nan(self, na_value, vtype):
)
def test_constructor_int_dtype_float(self, dtype):
# GH#18400
expected = NumericIndex([0, 1, 2, 3], dtype=dtype)
expected = Index([0, 1, 2, 3], dtype=dtype)
result = Index([0.0, 1.0, 2.0, 3.0], dtype=dtype)
tm.assert_index_equal(result, expected)

Expand Down Expand Up @@ -283,7 +282,7 @@ def test_constructor_int_dtype_nan_raises(self, dtype):
)
def test_constructor_dtypes_to_int(self, vals, any_int_numpy_dtype):
dtype = any_int_numpy_dtype
index = NumericIndex(vals, dtype=dtype)
index = Index(vals, dtype=dtype)
assert index.dtype == dtype

@pytest.mark.parametrize(
Expand All @@ -298,7 +297,7 @@ def test_constructor_dtypes_to_int(self, vals, any_int_numpy_dtype):
)
def test_constructor_dtypes_to_float(self, vals, float_numpy_dtype):
dtype = float_numpy_dtype
index = NumericIndex(vals, dtype=dtype)
index = Index(vals, dtype=dtype)
assert index.dtype == dtype

@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
TimedeltaIndex,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex


class TestTake:
Expand Down Expand Up @@ -141,7 +140,7 @@ def test_contains_with_float_index(self, any_real_numpy_dtype):
# GH#22085
dtype = any_real_numpy_dtype
data = [0, 1, 2, 3] if not is_float_dtype(dtype) else [0.1, 1.1, 2.2, 3.3]
index = NumericIndex(data, dtype=dtype)
index = Index(data, dtype=dtype)

if not is_float_dtype(index.dtype):
assert 1.1 not in index
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Series,
)
import pandas._testing as tm
from pandas.core.api import NumericIndex


def gen_obj(klass, index):
Expand Down Expand Up @@ -261,9 +260,9 @@ def test_slice_integer(self):
# oob indicates if we are out of bounds
# of positional indexing
for index, oob in [
(NumericIndex(np.arange(5, dtype=np.int64)), False),
(Index(np.arange(5, dtype=np.int64)), False),
(RangeIndex(5), False),
(NumericIndex(np.arange(5, dtype=np.int64) + 10), True),
(Index(np.arange(5, dtype=np.int64) + 10), True),
]:

# s is an in-range index
Expand Down