Skip to content

REF: test_invalid_dtype in numeric index tests #41359

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 4 commits into from
May 7, 2021
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
7 changes: 7 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 18 additions & 18 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Float64Index,
Index,
Int64Index,
RangeIndex,
Series,
UInt64Index,
)
Expand All @@ -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)
Expand Down Expand Up @@ -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__
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down