From bc3b915e0c7fc2e1ddd68950bb5ab7059b43d955 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 6 May 2021 21:51:55 +0100 Subject: [PATCH 1/4] REF: test_invalid_dtype in numeric index tests --- pandas/tests/indexes/common.py | 8 ++++ pandas/tests/indexes/numeric/test_numeric.py | 44 ++++++++++++++------ pandas/tests/indexes/ranges/test_range.py | 12 ++++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 45e1b615b1ade..e6647734eca7d 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -831,3 +831,11 @@ def test_arithmetic_explicit_conversions(self): a = np.zeros(5, dtype="float64") result = a - fidx tm.assert_index_equal(result, expected) + + def check_invalid_dtype(self, dtype): + # GH 29539 + with pytest.raises( + ValueError, + match=rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}", + ): + 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..a35312c993a7f 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, ) @@ -105,21 +104,16 @@ def test_constructor(self): assert pd.isna(result.values).all() @pytest.mark.parametrize( - "index, dtype", + "dtype", [ - (Int64Index, "float64"), - (UInt64Index, "categorical"), - (Float64Index, "datetime64"), - (RangeIndex, "float64"), + "int64", + "uint64", + "categorical", + "datetime64", ], ) - 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_invalid_dtype(self, dtype): + self.check_invalid_dtype(dtype) def test_constructor_invalid(self): index_cls = self._index_cls @@ -486,6 +480,18 @@ def test_coerce_list(self): arr = Index([1, 2, 3, 4], dtype=object) assert isinstance(arr, Index) + @pytest.mark.parametrize( + "dtype", + [ + "uint64", + "float64", + "categorical", + "datetime64", + ], + ) + def test_invalid_dtype(self, dtype): + self.check_invalid_dtype(dtype) + class TestUInt64Index(NumericInt): @@ -532,6 +538,18 @@ def test_constructor(self): res = Index([1, 2 ** 63 + 1], dtype=dtype) tm.assert_index_equal(res, idx) + @pytest.mark.parametrize( + "dtype", + [ + "int64", + "float64", + "categorical", + "datetime64", + ], + ) + def test_invalid_dtype(self, dtype): + self.check_invalid_dtype(dtype) + @pytest.mark.parametrize( "box", diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 3a4aa29ea620e..8e5abbdd262d5 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -376,6 +376,18 @@ def test_pickle_compat_construction(self): # RangeIndex() is a valid constructor pass + @pytest.mark.parametrize( + "dtype", + [ + "uint64", + "float64", + "categorical", + "datetime64", + ], + ) + def test_invalid_dtype(self, dtype): + self.check_invalid_dtype(dtype) + def test_slice_specialised(self, simple_index): index = simple_index index.name = "foo" From 6b4e351fd0d32a4a3560242e79e641993bf5dd39 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 6 May 2021 22:12:36 +0100 Subject: [PATCH 2/4] fix category --- pandas/tests/indexes/numeric/test_numeric.py | 6 +++--- pandas/tests/indexes/ranges/test_range.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index a35312c993a7f..44d1d63f74ccd 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -108,7 +108,7 @@ def test_constructor(self): [ "int64", "uint64", - "categorical", + "category", "datetime64", ], ) @@ -485,7 +485,7 @@ def test_coerce_list(self): [ "uint64", "float64", - "categorical", + "category", "datetime64", ], ) @@ -543,7 +543,7 @@ def test_constructor(self): [ "int64", "float64", - "categorical", + "category", "datetime64", ], ) diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 8e5abbdd262d5..a7d77b73f8d6a 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -381,7 +381,7 @@ def test_pickle_compat_construction(self): [ "uint64", "float64", - "categorical", + "category", "datetime64", ], ) From 502367d35b4e7cdf506a85e5bdb333e31d9fa05c Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 6 May 2021 23:25:13 +0100 Subject: [PATCH 3/4] better organisation --- pandas/tests/indexes/common.py | 3 +- pandas/tests/indexes/numeric/test_numeric.py | 54 +++++++------------- pandas/tests/indexes/ranges/test_range.py | 18 +++---- 3 files changed, 26 insertions(+), 49 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index e6647734eca7d..ade78b665d950 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -832,8 +832,9 @@ def test_arithmetic_explicit_conversions(self): result = a - fidx tm.assert_index_equal(result, expected) - def check_invalid_dtype(self, dtype): + def test_invalid_dtype(self, invalid_dtype): # GH 29539 + dtype = invalid_dtype with pytest.raises( ValueError, match=rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {dtype}", diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index 44d1d63f74ccd..e63aeba54fccd 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -19,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) @@ -103,18 +109,6 @@ def test_constructor(self): assert result.dtype == dtype assert pd.isna(result.values).all() - @pytest.mark.parametrize( - "dtype", - [ - "int64", - "uint64", - "category", - "datetime64", - ], - ) - def test_invalid_dtype(self, dtype): - self.check_invalid_dtype(dtype) - def test_constructor_invalid(self): index_cls = self._index_cls cls_name = index_cls.__name__ @@ -388,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) @@ -480,24 +480,18 @@ def test_coerce_list(self): arr = Index([1, 2, 3, 4], dtype=object) assert isinstance(arr, Index) - @pytest.mark.parametrize( - "dtype", - [ - "uint64", - "float64", - "category", - "datetime64", - ], - ) - def test_invalid_dtype(self, dtype): - self.check_invalid_dtype(dtype) - 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 @@ -538,18 +532,6 @@ def test_constructor(self): res = Index([1, 2 ** 63 + 1], dtype=dtype) tm.assert_index_equal(res, idx) - @pytest.mark.parametrize( - "dtype", - [ - "int64", - "float64", - "category", - "datetime64", - ], - ) - def test_invalid_dtype(self, dtype): - self.check_invalid_dtype(dtype) - @pytest.mark.parametrize( "box", diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index a7d77b73f8d6a..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) @@ -376,18 +382,6 @@ def test_pickle_compat_construction(self): # RangeIndex() is a valid constructor pass - @pytest.mark.parametrize( - "dtype", - [ - "uint64", - "float64", - "category", - "datetime64", - ], - ) - def test_invalid_dtype(self, dtype): - self.check_invalid_dtype(dtype) - def test_slice_specialised(self, simple_index): index = simple_index index.name = "foo" From a8f126a880df572db81134c6edd62ff357f961f1 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 6 May 2021 23:32:26 +0100 Subject: [PATCH 4/4] cleanup --- pandas/tests/indexes/common.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index ade78b665d950..857b136b67a0c 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -835,8 +835,6 @@ def test_arithmetic_explicit_conversions(self): def test_invalid_dtype(self, invalid_dtype): # GH 29539 dtype = invalid_dtype - with pytest.raises( - ValueError, - match=rf"Incorrect `dtype` passed: expected \w+(?: \w+)?, received {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)