Skip to content

TST/CLN: use dtype fixture in numeric tests #41380

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 2 commits into from
May 10, 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
8 changes: 7 additions & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Base:
_index_cls: Type[Index]

@pytest.fixture
def simple_index(self) -> Index:
def simple_index(self):
raise NotImplementedError("Method not implemented")

def create_index(self) -> Index:
Expand Down Expand Up @@ -772,6 +772,12 @@ class NumericBase(Base):
Base class for numeric index (incl. RangeIndex) sub-class tests.
"""

def test_constructor_unwraps_index(self, dtype):
idx = Index([1, 2], dtype=dtype)
result = self._index_cls(idx)
expected = np.array([1, 2], dtype=dtype)
tm.assert_numpy_array_equal(result._data, expected)

def test_where(self):
# Tested in numeric.test_indexing
pass
Expand Down
58 changes: 28 additions & 30 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

class TestFloat64Index(NumericBase):
_index_cls = Float64Index
_dtype = np.float64

@pytest.fixture
def dtype(self, request):
return np.float64

@pytest.fixture(
params=["int64", "uint64", "category", "datetime64"],
Expand All @@ -26,8 +29,8 @@ def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self) -> Index:
values = np.arange(5, dtype=self._dtype)
def simple_index(self, dtype):
values = np.arange(5, dtype=dtype)
return self._index_cls(values)

@pytest.fixture(
Expand Down Expand Up @@ -65,9 +68,8 @@ def check_coerce(self, a, b, is_float_index=True):
else:
self.check_is_index(b)

def test_constructor(self):
def test_constructor(self, dtype):
index_cls = self._index_cls
dtype = self._dtype

# explicit construction
index = index_cls([1, 2, 3, 4, 5])
Expand Down Expand Up @@ -204,8 +206,7 @@ def test_equals_numeric_other_index_type(self, other):
pd.timedelta_range("1 Day", periods=3),
],
)
def test_lookups_datetimelike_values(self, vals):
dtype = self._dtype
def test_lookups_datetimelike_values(self, vals, dtype):

# If we have datetime64 or timedelta64 values, make sure they are
# wrappped correctly GH#31163
Expand Down Expand Up @@ -277,14 +278,14 @@ def test_fillna_float64(self):


class NumericInt(NumericBase):
def test_view(self):
def test_view(self, dtype):
index_cls = self._index_cls

idx = index_cls([], name="Foo")
idx_view = idx.view()
assert idx_view.name == "Foo"

idx_view = idx.view(self._dtype)
idx_view = idx.view(dtype)
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"))

idx_view = idx.view(index_cls)
Expand Down Expand Up @@ -334,7 +335,7 @@ def test_logical_compat(self, simple_index):
assert idx.all() == idx.values.all()
assert idx.any() == idx.values.any()

def test_identical(self, simple_index):
def test_identical(self, simple_index, dtype):
index = simple_index

idx = Index(index.copy())
Expand All @@ -351,7 +352,7 @@ def test_identical(self, simple_index):
assert not idx.identical(index)
assert Index(same_values, name="foo", dtype=object).identical(idx)

assert not index.astype(dtype=object).identical(index.astype(dtype=self._dtype))
assert not index.astype(dtype=object).identical(index.astype(dtype=dtype))

def test_cant_or_shouldnt_cast(self):
msg = (
Expand Down Expand Up @@ -380,7 +381,10 @@ def test_prevent_casting(self, simple_index):

class TestInt64Index(NumericInt):
_index_cls = Int64Index
_dtype = np.int64

@pytest.fixture
def dtype(self):
return np.int64

@pytest.fixture(
params=["uint64", "float64", "category", "datetime64"],
Expand All @@ -389,18 +393,17 @@ 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)
def simple_index(self, dtype):
return self._index_cls(range(0, 20, 2), dtype=dtype)

@pytest.fixture(
params=[range(0, 20, 2), range(19, -1, -1)], ids=["index_inc", "index_dec"]
)
def index(self, request):
return self._index_cls(request.param)

def test_constructor(self):
def test_constructor(self, dtype):
index_cls = self._index_cls
dtype = self._dtype

# pass list, coerce fine
index = index_cls([-5, 0, 1, 2])
Expand Down Expand Up @@ -439,9 +442,8 @@ def test_constructor(self):
]:
tm.assert_index_equal(idx, expected)

def test_constructor_corner(self):
def test_constructor_corner(self, dtype):
index_cls = self._index_cls
dtype = self._dtype

arr = np.array([1, 2, 3, 4], dtype=object)
index = index_cls(arr)
Expand All @@ -465,26 +467,23 @@ def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
with pytest.raises(OverflowError, match=msg):
Index([-1], dtype=uint_dtype)

def test_constructor_unwraps_index(self):
idx = Index([1, 2])
result = self._index_cls(idx)
expected = np.array([1, 2], dtype=self._dtype)
tm.assert_numpy_array_equal(result._data, expected)

def test_coerce_list(self):
# coerce things
arr = Index([1, 2, 3, 4])
assert isinstance(arr, self._index_cls)

# but not if explicit dtype passed
arr = Index([1, 2, 3, 4], dtype=object)
assert isinstance(arr, Index)
assert type(arr) is Index


class TestUInt64Index(NumericInt):

_index_cls = UInt64Index
_dtype = np.uint64

@pytest.fixture
def dtype(self):
return np.uint64

@pytest.fixture(
params=["int64", "float64", "category", "datetime64"],
Expand All @@ -493,9 +492,9 @@ def invalid_dtype(self, request):
return request.param

@pytest.fixture
def simple_index(self) -> Index:
def simple_index(self, dtype):
# compat with shared Int64/Float64 tests
return self._index_cls(np.arange(5, dtype=self._dtype))
return self._index_cls(np.arange(5, dtype=dtype))

@pytest.fixture(
params=[
Expand All @@ -507,9 +506,8 @@ def simple_index(self) -> Index:
def index(self, request):
return self._index_cls(request.param)

def test_constructor(self):
def test_constructor(self, dtype):
index_cls = self._index_cls
dtype = self._dtype

idx = index_cls([1, 2, 3])
res = Index([1, 2, 3], dtype=dtype)
Expand Down
9 changes: 9 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,10 @@
class TestRangeIndex(NumericBase):
_index_cls = RangeIndex

@pytest.fixture
def dtype(self):
return np.int64

@pytest.fixture(
params=["uint64", "float64", "category", "datetime64"],
)
Expand All @@ -43,6 +47,11 @@ def simple_index(self) -> Index:
def index(self, request):
return request.param

def test_constructor_unwraps_index(self, dtype):
result = self._index_cls(1, 3)
expected = np.array([1, 2], dtype=dtype)
tm.assert_numpy_array_equal(result._data, expected)

def test_can_hold_identifiers(self, simple_index):
idx = simple_index
key = idx[0]
Expand Down