diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 45e1b615b1ade..857b136b67a0c 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -831,3 +831,10 @@ def test_arithmetic_explicit_conversions(self): a = np.zeros(5, dtype="float64") result = a - fidx tm.assert_index_equal(result, expected) + + def test_invalid_dtype(self, invalid_dtype): + # GH 29539 + dtype = invalid_dtype + msg = fr"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}" + with pytest.raises(ValueError, match=msg): + self._index_cls([1, 2, 3], dtype=dtype) diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index bfe06d74570da..e63aeba54fccd 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -8,7 +8,6 @@ Float64Index, Index, Int64Index, - RangeIndex, Series, UInt64Index, ) @@ -20,6 +19,12 @@ class TestFloat64Index(NumericBase): _index_cls = Float64Index _dtype = np.float64 + @pytest.fixture( + params=["int64", "uint64", "category", "datetime64"], + ) + def invalid_dtype(self, request): + return request.param + @pytest.fixture def simple_index(self) -> Index: values = np.arange(5, dtype=self._dtype) @@ -104,23 +109,6 @@ def test_constructor(self): assert result.dtype == dtype assert pd.isna(result.values).all() - @pytest.mark.parametrize( - "index, dtype", - [ - (Int64Index, "float64"), - (UInt64Index, "categorical"), - (Float64Index, "datetime64"), - (RangeIndex, "float64"), - ], - ) - def test_invalid_dtype(self, index, dtype): - # GH 29539 - with pytest.raises( - ValueError, - match=rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}", - ): - index([1, 2, 3], dtype=dtype) - def test_constructor_invalid(self): index_cls = self._index_cls cls_name = index_cls.__name__ @@ -394,6 +382,12 @@ class TestInt64Index(NumericInt): _index_cls = Int64Index _dtype = np.int64 + @pytest.fixture( + params=["uint64", "float64", "category", "datetime64"], + ) + def invalid_dtype(self, request): + return request.param + @pytest.fixture def simple_index(self) -> Index: return self._index_cls(range(0, 20, 2), dtype=self._dtype) @@ -492,6 +486,12 @@ class TestUInt64Index(NumericInt): _index_cls = UInt64Index _dtype = np.uint64 + @pytest.fixture( + params=["int64", "float64", "category", "datetime64"], + ) + def invalid_dtype(self, request): + return request.param + @pytest.fixture def simple_index(self) -> Index: # compat with shared Int64/Float64 tests diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 3a4aa29ea620e..12fce56ffcb21 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -23,6 +23,12 @@ class TestRangeIndex(NumericBase): _index_cls = RangeIndex + @pytest.fixture( + params=["uint64", "float64", "category", "datetime64"], + ) + def invalid_dtype(self, request): + return request.param + @pytest.fixture def simple_index(self) -> Index: return self._index_cls(start=0, stop=20, step=2)