Skip to content

Commit 3e54588

Browse files
committed
TST/CLS: use dtype fixture numeric tests
1 parent 67c9385 commit 3e54588

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

pandas/tests/indexes/common.py

+6
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,12 @@ class NumericBase(Base):
772772
Base class for numeric index (incl. RangeIndex) sub-class tests.
773773
"""
774774

775+
def test_constructor_unwraps_index(self, dtype):
776+
idx = Index([1, 2], dtype=dtype)
777+
result = self._index_cls(idx)
778+
expected = np.array([1, 2], dtype=dtype)
779+
tm.assert_numpy_array_equal(result._data, expected)
780+
775781
def test_where(self):
776782
# Tested in numeric.test_indexing
777783
pass

pandas/tests/indexes/numeric/test_numeric.py

+28-30
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
class TestFloat64Index(NumericBase):
1919
_index_cls = Float64Index
20-
_dtype = np.float64
20+
21+
@pytest.fixture
22+
def dtype(self, request):
23+
return np.float64
2124

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

2831
@pytest.fixture
29-
def simple_index(self) -> Index:
30-
values = np.arange(5, dtype=self._dtype)
32+
def simple_index(self, dtype) -> Index:
33+
values = np.arange(5, dtype=dtype)
3134
return self._index_cls(values)
3235

3336
@pytest.fixture(
@@ -65,9 +68,8 @@ def check_coerce(self, a, b, is_float_index=True):
6568
else:
6669
self.check_is_index(b)
6770

68-
def test_constructor(self):
71+
def test_constructor(self, dtype):
6972
index_cls = self._index_cls
70-
dtype = self._dtype
7173

7274
# explicit construction
7375
index = index_cls([1, 2, 3, 4, 5])
@@ -204,8 +206,7 @@ def test_equals_numeric_other_index_type(self, other):
204206
pd.timedelta_range("1 Day", periods=3),
205207
],
206208
)
207-
def test_lookups_datetimelike_values(self, vals):
208-
dtype = self._dtype
209+
def test_lookups_datetimelike_values(self, vals, dtype):
209210

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

278279

279280
class NumericInt(NumericBase):
280-
def test_view(self):
281+
def test_view(self, dtype):
281282
index_cls = self._index_cls
282283

283284
idx = index_cls([], name="Foo")
284285
idx_view = idx.view()
285286
assert idx_view.name == "Foo"
286287

287-
idx_view = idx.view(self._dtype)
288+
idx_view = idx.view(dtype)
288289
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"))
289290

290291
idx_view = idx.view(index_cls)
@@ -334,7 +335,7 @@ def test_logical_compat(self, simple_index):
334335
assert idx.all() == idx.values.all()
335336
assert idx.any() == idx.values.any()
336337

337-
def test_identical(self, simple_index):
338+
def test_identical(self, simple_index, dtype):
338339
index = simple_index
339340

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

354-
assert not index.astype(dtype=object).identical(index.astype(dtype=self._dtype))
355+
assert not index.astype(dtype=object).identical(index.astype(dtype=dtype))
355356

356357
def test_cant_or_shouldnt_cast(self):
357358
msg = (
@@ -380,7 +381,10 @@ def test_prevent_casting(self, simple_index):
380381

381382
class TestInt64Index(NumericInt):
382383
_index_cls = Int64Index
383-
_dtype = np.int64
384+
385+
@pytest.fixture
386+
def dtype(self):
387+
return np.int64
384388

385389
@pytest.fixture(
386390
params=["uint64", "float64", "category", "datetime64"],
@@ -389,18 +393,17 @@ def invalid_dtype(self, request):
389393
return request.param
390394

391395
@pytest.fixture
392-
def simple_index(self) -> Index:
393-
return self._index_cls(range(0, 20, 2), dtype=self._dtype)
396+
def simple_index(self, dtype) -> Index:
397+
return self._index_cls(range(0, 20, 2), dtype=dtype)
394398

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

401-
def test_constructor(self):
405+
def test_constructor(self, dtype):
402406
index_cls = self._index_cls
403-
dtype = self._dtype
404407

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

442-
def test_constructor_corner(self):
445+
def test_constructor_corner(self, dtype):
443446
index_cls = self._index_cls
444-
dtype = self._dtype
445447

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

468-
def test_constructor_unwraps_index(self):
469-
idx = Index([1, 2])
470-
result = self._index_cls(idx)
471-
expected = np.array([1, 2], dtype=self._dtype)
472-
tm.assert_numpy_array_equal(result._data, expected)
473-
474470
def test_coerce_list(self):
475471
# coerce things
476472
arr = Index([1, 2, 3, 4])
477473
assert isinstance(arr, self._index_cls)
478474

479475
# but not if explicit dtype passed
480476
arr = Index([1, 2, 3, 4], dtype=object)
481-
assert isinstance(arr, Index)
477+
assert type(arr) is Index
482478

483479

484480
class TestUInt64Index(NumericInt):
485481

486482
_index_cls = UInt64Index
487-
_dtype = np.uint64
483+
484+
@pytest.fixture
485+
def dtype(self):
486+
return np.uint64
488487

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

495494
@pytest.fixture
496-
def simple_index(self) -> Index:
495+
def simple_index(self, dtype) -> Index:
497496
# compat with shared Int64/Float64 tests
498-
return self._index_cls(np.arange(5, dtype=self._dtype))
497+
return self._index_cls(np.arange(5, dtype=dtype))
499498

500499
@pytest.fixture(
501500
params=[
@@ -507,9 +506,8 @@ def simple_index(self) -> Index:
507506
def index(self, request):
508507
return self._index_cls(request.param)
509508

510-
def test_constructor(self):
509+
def test_constructor(self, dtype):
511510
index_cls = self._index_cls
512-
dtype = self._dtype
513511

514512
idx = index_cls([1, 2, 3])
515513
res = Index([1, 2, 3], dtype=dtype)

pandas/tests/indexes/ranges/test_range.py

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
class TestRangeIndex(NumericBase):
2424
_index_cls = RangeIndex
2525

26+
@pytest.fixture
27+
def dtype(self):
28+
return np.int64
29+
2630
@pytest.fixture(
2731
params=["uint64", "float64", "category", "datetime64"],
2832
)
@@ -43,6 +47,11 @@ def simple_index(self) -> Index:
4347
def index(self, request):
4448
return request.param
4549

50+
def test_constructor_unwraps_index(self, dtype):
51+
result = self._index_cls(1, 3)
52+
expected = np.array([1, 2], dtype=dtype)
53+
tm.assert_numpy_array_equal(result._data, expected)
54+
4655
def test_can_hold_identifiers(self, simple_index):
4756
idx = simple_index
4857
key = idx[0]

0 commit comments

Comments
 (0)