Skip to content

Commit e927af2

Browse files
authored
REF: test_invalid_dtype in numeric index tests (#41359)
1 parent 6ea277f commit e927af2

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

pandas/tests/indexes/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,10 @@ def test_arithmetic_explicit_conversions(self):
831831
a = np.zeros(5, dtype="float64")
832832
result = a - fidx
833833
tm.assert_index_equal(result, expected)
834+
835+
def test_invalid_dtype(self, invalid_dtype):
836+
# GH 29539
837+
dtype = invalid_dtype
838+
msg = fr"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}"
839+
with pytest.raises(ValueError, match=msg):
840+
self._index_cls([1, 2, 3], dtype=dtype)

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Float64Index,
99
Index,
1010
Int64Index,
11-
RangeIndex,
1211
Series,
1312
UInt64Index,
1413
)
@@ -20,6 +19,12 @@ class TestFloat64Index(NumericBase):
2019
_index_cls = Float64Index
2120
_dtype = np.float64
2221

22+
@pytest.fixture(
23+
params=["int64", "uint64", "category", "datetime64"],
24+
)
25+
def invalid_dtype(self, request):
26+
return request.param
27+
2328
@pytest.fixture
2429
def simple_index(self) -> Index:
2530
values = np.arange(5, dtype=self._dtype)
@@ -104,23 +109,6 @@ def test_constructor(self):
104109
assert result.dtype == dtype
105110
assert pd.isna(result.values).all()
106111

107-
@pytest.mark.parametrize(
108-
"index, dtype",
109-
[
110-
(Int64Index, "float64"),
111-
(UInt64Index, "categorical"),
112-
(Float64Index, "datetime64"),
113-
(RangeIndex, "float64"),
114-
],
115-
)
116-
def test_invalid_dtype(self, index, dtype):
117-
# GH 29539
118-
with pytest.raises(
119-
ValueError,
120-
match=rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}",
121-
):
122-
index([1, 2, 3], dtype=dtype)
123-
124112
def test_constructor_invalid(self):
125113
index_cls = self._index_cls
126114
cls_name = index_cls.__name__
@@ -394,6 +382,12 @@ class TestInt64Index(NumericInt):
394382
_index_cls = Int64Index
395383
_dtype = np.int64
396384

385+
@pytest.fixture(
386+
params=["uint64", "float64", "category", "datetime64"],
387+
)
388+
def invalid_dtype(self, request):
389+
return request.param
390+
397391
@pytest.fixture
398392
def simple_index(self) -> Index:
399393
return self._index_cls(range(0, 20, 2), dtype=self._dtype)
@@ -492,6 +486,12 @@ class TestUInt64Index(NumericInt):
492486
_index_cls = UInt64Index
493487
_dtype = np.uint64
494488

489+
@pytest.fixture(
490+
params=["int64", "float64", "category", "datetime64"],
491+
)
492+
def invalid_dtype(self, request):
493+
return request.param
494+
495495
@pytest.fixture
496496
def simple_index(self) -> Index:
497497
# compat with shared Int64/Float64 tests

pandas/tests/indexes/ranges/test_range.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
class TestRangeIndex(NumericBase):
2424
_index_cls = RangeIndex
2525

26+
@pytest.fixture(
27+
params=["uint64", "float64", "category", "datetime64"],
28+
)
29+
def invalid_dtype(self, request):
30+
return request.param
31+
2632
@pytest.fixture
2733
def simple_index(self) -> Index:
2834
return self._index_cls(start=0, stop=20, step=2)

0 commit comments

Comments
 (0)