Skip to content

Commit 852518e

Browse files
authored
DEPR: Remove NumericIndex from tests/indexes/numeric.py (#51013)
1 parent b3cb116 commit 852518e

File tree

3 files changed

+23
-54
lines changed

3 files changed

+23
-54
lines changed

pandas/tests/indexes/common.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -935,15 +935,13 @@ def test_arithmetic_explicit_conversions(self):
935935
result = a - fidx
936936
tm.assert_index_equal(result, expected)
937937

938-
def test_invalid_dtype(self, invalid_dtype):
939-
# GH 29539
940-
dtype = invalid_dtype
941-
msg = rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}"
942-
with pytest.raises(ValueError, match=msg):
943-
self._index_cls([1, 2, 3], dtype=dtype)
944-
945938
@pytest.mark.parametrize("complex_dtype", [np.complex64, np.complex128])
946939
def test_astype_to_complex(self, complex_dtype, simple_index):
947940
result = simple_index.astype(complex_dtype)
948941

949942
assert type(result) is Index and result.dtype == complex_dtype
943+
944+
def test_cast_string(self, dtype):
945+
result = self._index_cls(["0", "1", "2"], dtype=dtype)
946+
expected = self._index_cls([0, 1, 2], dtype=dtype)
947+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/numeric/test_numeric.py

+14-46
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._libs.tslibs import Timestamp
5-
64
import pandas as pd
75
from pandas import (
86
Index,
97
Series,
108
)
119
import pandas._testing as tm
12-
from pandas.core.indexes.api import NumericIndex
1310
from pandas.tests.indexes.common import NumericBase
1411

1512

1613
class TestFloatNumericIndex(NumericBase):
17-
_index_cls = NumericIndex
14+
_index_cls = Index
1815

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

23-
@pytest.fixture(params=["category", "datetime64", "object"])
24-
def invalid_dtype(self, request):
25-
return request.param
26-
2720
@pytest.fixture
2821
def simple_index(self, dtype):
2922
values = np.arange(5, dtype=dtype)
@@ -50,19 +43,17 @@ def float_index(self, dtype):
5043
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)
5144

5245
def test_repr_roundtrip(self, index):
53-
tm.assert_index_equal(eval(repr(index)), index, exact=True)
46+
from pandas.core.api import NumericIndex # noqa: F401
5447

55-
def check_is_index(self, idx):
56-
assert isinstance(idx, Index)
57-
assert not isinstance(idx, self._index_cls)
48+
tm.assert_index_equal(eval(repr(index)), index, exact=True)
5849

5950
def check_coerce(self, a, b, is_float_index=True):
6051
assert a.equals(b)
6152
tm.assert_index_equal(a, b, exact=False)
6253
if is_float_index:
6354
assert isinstance(b, self._index_cls)
6455
else:
65-
self.check_is_index(b)
56+
assert type(b) is Index
6657

6758
def test_constructor_from_list_no_dtype(self):
6859
index = self._index_cls([1.5, 2.5, 3.5])
@@ -110,7 +101,6 @@ def test_constructor(self, dtype):
110101
def test_constructor_invalid(self):
111102
index_cls = self._index_cls
112103
cls_name = index_cls.__name__
113-
114104
# invalid
115105
msg = (
116106
rf"{cls_name}\(\.\.\.\) must be called with a collection of "
@@ -119,13 +109,6 @@ def test_constructor_invalid(self):
119109
with pytest.raises(TypeError, match=msg):
120110
index_cls(0.0)
121111

122-
msg = f"data is not compatible with {index_cls.__name__}"
123-
with pytest.raises(ValueError, match=msg):
124-
index_cls(["a", "b", 0.0])
125-
126-
with pytest.raises(ValueError, match=msg):
127-
index_cls([Timestamp("20130101")])
128-
129112
def test_constructor_coerce(self, mixed_index, float_index):
130113

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

255238

256239
class NumericInt(NumericBase):
240+
_index_cls = Index
241+
257242
def test_is_monotonic(self):
258243
index_cls = self._index_cls
259244

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

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

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

323308
# can't
324309
data = ["foo", "bar", "baz"]
325310
with pytest.raises(ValueError, match=msg):
326-
self._index_cls(data)
327-
328-
# shouldn't
329-
data = ["0", "1", "2"]
330-
with pytest.raises(ValueError, match=msg):
331-
self._index_cls(data)
311+
self._index_cls(data, dtype=dtype)
332312

333313
def test_view_index(self, simple_index):
334314
index = simple_index
@@ -341,16 +321,10 @@ def test_prevent_casting(self, simple_index):
341321

342322

343323
class TestIntNumericIndex(NumericInt):
344-
_index_cls = NumericIndex
345-
346324
@pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8])
347325
def dtype(self, request):
348326
return request.param
349327

350-
@pytest.fixture(params=["category", "datetime64", "object"])
351-
def invalid_dtype(self, request):
352-
return request.param
353-
354328
@pytest.fixture
355329
def simple_index(self, dtype):
356330
return self._index_cls(range(0, 20, 2), dtype=dtype)
@@ -427,7 +401,8 @@ def test_constructor_corner(self, dtype):
427401

428402
# preventing casting
429403
arr = np.array([1, "2", 3, "4"], dtype=object)
430-
with pytest.raises(TypeError, match="casting"):
404+
msg = "Trying to coerce float values to integers"
405+
with pytest.raises(ValueError, match=msg):
431406
index_cls(arr, dtype=dtype)
432407

433408
def test_constructor_coercion_signed_to_unsigned(
@@ -468,7 +443,7 @@ def test_coerce_list(self):
468443
class TestFloat16Index:
469444
# float 16 indexes not supported
470445
# GH 49535
471-
_index_cls = NumericIndex
446+
_index_cls = Index
472447

473448
def test_constructor(self):
474449
index_cls = self._index_cls
@@ -504,17 +479,10 @@ def test_constructor(self):
504479

505480

506481
class TestUIntNumericIndex(NumericInt):
507-
508-
_index_cls = NumericIndex
509-
510482
@pytest.fixture(params=[np.uint64])
511483
def dtype(self, request):
512484
return request.param
513485

514-
@pytest.fixture(params=["category", "datetime64", "object"])
515-
def invalid_dtype(self, request):
516-
return request.param
517-
518486
@pytest.fixture
519487
def simple_index(self, dtype):
520488
# compat with shared Int64/Float64 tests
@@ -583,8 +551,8 @@ def test_map_dtype_inference_unsigned_to_signed():
583551

584552
def test_map_dtype_inference_overflows():
585553
# GH#44609 case where we have to upcast
586-
idx = NumericIndex(np.array([1, 2, 3], dtype=np.int8))
554+
idx = Index(np.array([1, 2, 3], dtype=np.int8))
587555
result = idx.map(lambda x: x * 1000)
588556
# TODO: we could plausibly try to infer down to int16 here
589-
expected = NumericIndex([1000, 2000, 3000], dtype=np.int64)
557+
expected = Index([1000, 2000, 3000], dtype=np.int64)
590558
tm.assert_index_equal(result, expected)

pandas/tests/indexes/ranges/test_range.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def invalid_dtype(self, request):
2929
return request.param
3030

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

3535
@pytest.fixture(
@@ -612,3 +612,6 @@ def test_sort_values_key(self):
612612
result = values.sort_values(key=lambda x: x.map(sort_order))
613613
expected = Index([4, 8, 6, 0, 2], dtype="int64")
614614
tm.assert_index_equal(result, expected, check_exact=True)
615+
616+
def test_cast_string(self, dtype):
617+
pytest.skip("casting of strings not relevant for RangeIndex")

0 commit comments

Comments
 (0)