Skip to content

DEPR: Remove NumericIndex from tests/indexes/numeric.py #51013

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
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
12 changes: 5 additions & 7 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,15 +929,13 @@ def test_arithmetic_explicit_conversions(self):
result = a - fidx
tm.assert_index_equal(result, expected)

def test_invalid_dtype(self, invalid_dtype):
# GH 29539
dtype = invalid_dtype
msg = rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}"
with pytest.raises(ValueError, match=msg):
self._index_cls([1, 2, 3], dtype=dtype)

@pytest.mark.parametrize("complex_dtype", [np.complex64, np.complex128])
def test_astype_to_complex(self, complex_dtype, simple_index):
result = simple_index.astype(complex_dtype)

assert type(result) is Index and result.dtype == complex_dtype

def test_cast_string(self, dtype):
result = self._index_cls(["0", "1", "2"], dtype=dtype)
expected = self._index_cls([0, 1, 2], dtype=dtype)
tm.assert_index_equal(result, expected)
60 changes: 14 additions & 46 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import numpy as np
import pytest

from pandas._libs.tslibs import Timestamp

import pandas as pd
from pandas import (
Index,
Series,
)
import pandas._testing as tm
from pandas.core.indexes.api import NumericIndex
from pandas.tests.indexes.common import NumericBase


class TestFloatNumericIndex(NumericBase):
_index_cls = NumericIndex
_index_cls = Index

@pytest.fixture(params=[np.float64, np.float32])
def dtype(self, request):
return request.param

@pytest.fixture(params=["category", "datetime64", "object"])
def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self, dtype):
values = np.arange(5, dtype=dtype)
Expand All @@ -50,19 +43,17 @@ def float_index(self, dtype):
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)

def test_repr_roundtrip(self, index):
tm.assert_index_equal(eval(repr(index)), index, exact=True)
from pandas.core.api import NumericIndex # noqa: F401
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this used for?

Copy link
Contributor Author

@topper-123 topper-123 Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just temporary crutch until we remove NumericIndex. So it will be gone again in a few PRs.

The issue is that ATM repr(index) gives e.g. "NumericIndex([1, 2, 3], dtype='int32')". To call eval on this, we need NumericIndex the in namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got you, thx


def check_is_index(self, idx):
assert isinstance(idx, Index)
assert not isinstance(idx, self._index_cls)
tm.assert_index_equal(eval(repr(index)), index, exact=True)

def check_coerce(self, a, b, is_float_index=True):
assert a.equals(b)
tm.assert_index_equal(a, b, exact=False)
if is_float_index:
assert isinstance(b, self._index_cls)
else:
self.check_is_index(b)
assert type(b) is Index

def test_constructor_from_list_no_dtype(self):
index = self._index_cls([1.5, 2.5, 3.5])
Expand Down Expand Up @@ -110,7 +101,6 @@ def test_constructor(self, dtype):
def test_constructor_invalid(self):
index_cls = self._index_cls
cls_name = index_cls.__name__

# invalid
msg = (
rf"{cls_name}\(\.\.\.\) must be called with a collection of "
Expand All @@ -119,13 +109,6 @@ def test_constructor_invalid(self):
with pytest.raises(TypeError, match=msg):
index_cls(0.0)

msg = f"data is not compatible with {index_cls.__name__}"
with pytest.raises(ValueError, match=msg):
index_cls(["a", "b", 0.0])

with pytest.raises(ValueError, match=msg):
index_cls([Timestamp("20130101")])

def test_constructor_coerce(self, mixed_index, float_index):

self.check_coerce(mixed_index, Index([1.5, 2, 3, 4, 5]))
Expand Down Expand Up @@ -254,6 +237,8 @@ def test_fillna_float64(self):


class NumericInt(NumericBase):
_index_cls = Index

def test_is_monotonic(self):
index_cls = self._index_cls

Expand Down Expand Up @@ -317,18 +302,13 @@ def test_identical(self, simple_index, dtype):

assert not index.astype(dtype=object).identical(index.astype(dtype=dtype))

def test_cant_or_shouldnt_cast(self):
msg = f"data is not compatible with {self._index_cls.__name__}"
def test_cant_or_shouldnt_cast(self, dtype):
msg = r"invalid literal for int\(\) with base 10: 'foo'"

# can't
data = ["foo", "bar", "baz"]
with pytest.raises(ValueError, match=msg):
self._index_cls(data)

# shouldn't
data = ["0", "1", "2"]
with pytest.raises(ValueError, match=msg):
self._index_cls(data)
self._index_cls(data, dtype=dtype)

def test_view_index(self, simple_index):
index = simple_index
Expand All @@ -341,16 +321,10 @@ def test_prevent_casting(self, simple_index):


class TestIntNumericIndex(NumericInt):
_index_cls = NumericIndex

@pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8])
def dtype(self, request):
return request.param

@pytest.fixture(params=["category", "datetime64", "object"])
def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self, dtype):
return self._index_cls(range(0, 20, 2), dtype=dtype)
Expand Down Expand Up @@ -427,7 +401,8 @@ def test_constructor_corner(self, dtype):

# preventing casting
arr = np.array([1, "2", 3, "4"], dtype=object)
with pytest.raises(TypeError, match="casting"):
msg = "Trying to coerce float values to integers"
with pytest.raises(ValueError, match=msg):
index_cls(arr, dtype=dtype)

def test_constructor_coercion_signed_to_unsigned(
Expand Down Expand Up @@ -468,7 +443,7 @@ def test_coerce_list(self):
class TestFloat16Index:
# float 16 indexes not supported
# GH 49535
_index_cls = NumericIndex
_index_cls = Index

def test_constructor(self):
index_cls = self._index_cls
Expand Down Expand Up @@ -504,17 +479,10 @@ def test_constructor(self):


class TestUIntNumericIndex(NumericInt):

_index_cls = NumericIndex

@pytest.fixture(params=[np.uint64])
def dtype(self, request):
return request.param

@pytest.fixture(params=["category", "datetime64", "object"])
def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self, dtype):
# compat with shared Int64/Float64 tests
Expand Down Expand Up @@ -583,8 +551,8 @@ def test_map_dtype_inference_unsigned_to_signed():

def test_map_dtype_inference_overflows():
# GH#44609 case where we have to upcast
idx = NumericIndex(np.array([1, 2, 3], dtype=np.int8))
idx = Index(np.array([1, 2, 3], dtype=np.int8))
result = idx.map(lambda x: x * 1000)
# TODO: we could plausibly try to infer down to int16 here
expected = NumericIndex([1000, 2000, 3000], dtype=np.int64)
expected = Index([1000, 2000, 3000], dtype=np.int64)
tm.assert_index_equal(result, expected)
5 changes: 4 additions & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self) -> Index:
def simple_index(self):
return self._index_cls(start=0, stop=20, step=2)

@pytest.fixture(
Expand Down Expand Up @@ -612,3 +612,6 @@ def test_sort_values_key(self):
result = values.sort_values(key=lambda x: x.map(sort_order))
expected = Index([4, 8, 6, 0, 2], dtype="int64")
tm.assert_index_equal(result, expected, check_exact=True)

def test_cast_string(self, dtype):
pytest.skip("casting of strings not relevant for RangeIndex")