Skip to content

Commit 2f312da

Browse files
authored
TST: Remove inheritance from pandas/tests/indexes (#53544)
* Refactor test/indexes * Address failures
1 parent 5ceebe9 commit 2f312da

File tree

10 files changed

+253
-348
lines changed

10 files changed

+253
-348
lines changed

pandas/tests/indexes/categorical/test_category.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,15 @@
1414
CategoricalIndex,
1515
Index,
1616
)
17-
from pandas.tests.indexes.common import Base
1817

1918

20-
class TestCategoricalIndex(Base):
21-
_index_cls = CategoricalIndex
22-
19+
class TestCategoricalIndex:
2320
@pytest.fixture
2421
def simple_index(self) -> CategoricalIndex:
25-
return self._index_cls(list("aabbca"), categories=list("cab"), ordered=False)
26-
27-
@pytest.fixture
28-
def index(self):
29-
return tm.makeCategoricalIndex(100)
30-
31-
def create_index(self, *, categories=None, ordered=False):
32-
if categories is None:
33-
categories = list("cab")
34-
return CategoricalIndex(list("aabbca"), categories=categories, ordered=ordered)
22+
return CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
3523

3624
def test_can_hold_identifiers(self):
37-
idx = self.create_index(categories=list("abcd"))
25+
idx = CategoricalIndex(list("aabbca"), categories=None, ordered=False)
3826
key = idx[0]
3927
assert idx._can_hold_identifiers_and_holds_name(key) is True
4028

@@ -247,12 +235,13 @@ def test_identical(self):
247235
assert ci1.identical(ci1.copy())
248236
assert not ci1.identical(ci2)
249237

250-
def test_ensure_copied_data(self, index):
238+
def test_ensure_copied_data(self):
251239
# gh-12309: Check the "copy" argument of each
252240
# Index.__new__ is honored.
253241
#
254242
# Must be tested separately from other indexes because
255243
# self.values is not an ndarray.
244+
index = tm.makeCategoricalIndex(10)
256245

257246
result = CategoricalIndex(index.values, copy=True)
258247
tm.assert_index_equal(index, result)
@@ -267,18 +256,8 @@ def test_frame_repr(self):
267256
expected = " A\na 1\nb 2\nc 3"
268257
assert result == expected
269258

270-
def test_reindex_base(self):
271-
# See test_reindex.py
272-
pass
273-
274-
def test_map_str(self):
275-
# See test_map.py
276-
pass
277-
278259

279260
class TestCategoricalIndex2:
280-
# Tests that are not overriding a test in Base
281-
282261
def test_view_i8(self):
283262
# GH#25464
284263
ci = tm.makeCategoricalIndex(100)
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
""" generic tests from the Datetimelike class """
2-
import pytest
2+
from pandas import date_range
33

4-
from pandas import (
5-
DatetimeIndex,
6-
date_range,
7-
)
8-
import pandas._testing as tm
9-
from pandas.tests.indexes.datetimelike import DatetimeLike
104

11-
12-
class TestDatetimeIndex(DatetimeLike):
13-
_index_cls = DatetimeIndex
14-
15-
@pytest.fixture
16-
def simple_index(self) -> DatetimeIndex:
17-
return date_range("20130101", periods=5)
18-
19-
@pytest.fixture(
20-
params=[tm.makeDateIndex(10), date_range("20130110", periods=10, freq="-1D")],
21-
ids=["index_inc", "index_dec"],
22-
)
23-
def index(self, request):
24-
return request.param
25-
26-
def test_format(self, simple_index):
5+
class TestDatetimeIndex:
6+
def test_format(self):
277
# GH35439
28-
idx = simple_index
8+
idx = date_range("20130101", periods=5)
299
expected = [f"{x:%Y-%m-%d}" for x in idx]
3010
assert idx.format() == expected
31-
32-
def test_shift(self):
33-
pass # handled in test_ops
34-
35-
def test_intersection(self):
36-
pass # handled in test_setops
37-
38-
def test_union(self):
39-
pass # handled in test_setops

pandas/tests/indexes/interval/test_base.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,24 @@
33

44
from pandas import IntervalIndex
55
import pandas._testing as tm
6-
from pandas.tests.indexes.common import Base
76

87

9-
class TestBase(Base):
8+
class TestInterval:
109
"""
1110
Tests specific to the shared common index tests; unrelated tests should be placed
1211
in test_interval.py or the specific test file (e.g. test_astype.py)
1312
"""
1413

15-
_index_cls = IntervalIndex
16-
1714
@pytest.fixture
1815
def simple_index(self) -> IntervalIndex:
19-
return self._index_cls.from_breaks(range(11), closed="right")
16+
return IntervalIndex.from_breaks(range(11), closed="right")
2017

2118
@pytest.fixture
2219
def index(self):
2320
return tm.makeIntervalIndex(10)
2421

25-
def create_index(self, *, closed="right"):
26-
return IntervalIndex.from_breaks(range(11), closed=closed)
27-
28-
def test_repr_max_seq_item_setting(self):
29-
# override base test: not a valid repr as we use interval notation
30-
pass
31-
32-
def test_repr_roundtrip(self):
33-
# override base test: not a valid repr as we use interval notation
34-
pass
35-
3622
def test_take(self, closed):
37-
index = self.create_index(closed=closed)
23+
index = IntervalIndex.from_breaks(range(11), closed=closed)
3824

3925
result = index.take(range(10))
4026
tm.assert_index_equal(result, index)

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@
77
Series,
88
)
99
import pandas._testing as tm
10-
from pandas.tests.indexes.common import NumericBase
1110

1211

13-
class TestFloatNumericIndex(NumericBase):
14-
_index_cls = Index
15-
12+
class TestFloatNumericIndex:
1613
@pytest.fixture(params=[np.float64, np.float32])
1714
def dtype(self, request):
1815
return request.param
1916

2017
@pytest.fixture
2118
def simple_index(self, dtype):
2219
values = np.arange(5, dtype=dtype)
23-
return self._index_cls(values)
20+
return Index(values)
2421

2522
@pytest.fixture(
2623
params=[
@@ -32,15 +29,15 @@ def simple_index(self, dtype):
3229
ids=["mixed", "float", "mixed_dec", "float_dec"],
3330
)
3431
def index(self, request, dtype):
35-
return self._index_cls(request.param, dtype=dtype)
32+
return Index(request.param, dtype=dtype)
3633

3734
@pytest.fixture
3835
def mixed_index(self, dtype):
39-
return self._index_cls([1.5, 2, 3, 4, 5], dtype=dtype)
36+
return Index([1.5, 2, 3, 4, 5], dtype=dtype)
4037

4138
@pytest.fixture
4239
def float_index(self, dtype):
43-
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)
40+
return Index([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)
4441

4542
def test_repr_roundtrip(self, index):
4643
tm.assert_index_equal(eval(repr(index)), index, exact=True)
@@ -49,16 +46,16 @@ def check_coerce(self, a, b, is_float_index=True):
4946
assert a.equals(b)
5047
tm.assert_index_equal(a, b, exact=False)
5148
if is_float_index:
52-
assert isinstance(b, self._index_cls)
49+
assert isinstance(b, Index)
5350
else:
5451
assert type(b) is Index
5552

5653
def test_constructor_from_list_no_dtype(self):
57-
index = self._index_cls([1.5, 2.5, 3.5])
54+
index = Index([1.5, 2.5, 3.5])
5855
assert index.dtype == np.float64
5956

6057
def test_constructor(self, dtype):
61-
index_cls = self._index_cls
58+
index_cls = Index
6259

6360
# explicit construction
6461
index = index_cls([1, 2, 3, 4, 5], dtype=dtype)
@@ -97,7 +94,7 @@ def test_constructor(self, dtype):
9794
assert pd.isna(result.values).all()
9895

9996
def test_constructor_invalid(self):
100-
index_cls = self._index_cls
97+
index_cls = Index
10198
cls_name = index_cls.__name__
10299
# invalid
103100
msg = (
@@ -131,7 +128,7 @@ def test_type_coercion_fail(self, any_int_numpy_dtype):
131128
Index([1, 2, 3.5], dtype=any_int_numpy_dtype)
132129

133130
def test_equals_numeric(self):
134-
index_cls = self._index_cls
131+
index_cls = Index
135132

136133
idx = index_cls([1.0, 2.0])
137134
assert idx.equals(idx)
@@ -156,7 +153,7 @@ def test_equals_numeric(self):
156153
),
157154
)
158155
def test_equals_numeric_other_index_type(self, other):
159-
idx = self._index_cls([1.0, 2.0])
156+
idx = Index([1.0, 2.0])
160157
assert idx.equals(other)
161158
assert other.equals(idx)
162159

@@ -198,13 +195,13 @@ def test_lookups_datetimelike_values(self, vals, dtype):
198195
assert isinstance(result, type(expected)) and result == expected
199196

200197
def test_doesnt_contain_all_the_things(self):
201-
idx = self._index_cls([np.nan])
198+
idx = Index([np.nan])
202199
assert not idx.isin([0]).item()
203200
assert not idx.isin([1]).item()
204201
assert idx.isin([np.nan]).item()
205202

206203
def test_nan_multiple_containment(self):
207-
index_cls = self._index_cls
204+
index_cls = Index
208205

209206
idx = index_cls([1.0, np.nan])
210207
tm.assert_numpy_array_equal(idx.isin([1.0]), np.array([True, False]))
@@ -215,7 +212,7 @@ def test_nan_multiple_containment(self):
215212
tm.assert_numpy_array_equal(idx.isin([np.nan]), np.array([False, False]))
216213

217214
def test_fillna_float64(self):
218-
index_cls = self._index_cls
215+
index_cls = Index
219216
# GH 11343
220217
idx = Index([1.0, np.nan, 3.0], dtype=float, name="x")
221218
# can't downcast
@@ -231,11 +228,17 @@ def test_fillna_float64(self):
231228
tm.assert_index_equal(idx.fillna("obj"), exp, exact=True)
232229

233230

234-
class NumericInt(NumericBase):
235-
_index_cls = Index
231+
class TestNumericInt:
232+
@pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8, np.uint64])
233+
def dtype(self, request):
234+
return request.param
235+
236+
@pytest.fixture
237+
def simple_index(self, dtype):
238+
return Index(range(0, 20, 2), dtype=dtype)
236239

237240
def test_is_monotonic(self):
238-
index_cls = self._index_cls
241+
index_cls = Index
239242

240243
index = index_cls([1, 2, 3, 4])
241244
assert index.is_monotonic_increasing is True
@@ -257,7 +260,7 @@ def test_is_monotonic(self):
257260
assert index._is_strictly_monotonic_decreasing is True
258261

259262
def test_is_strictly_monotonic(self):
260-
index_cls = self._index_cls
263+
index_cls = Index
261264

262265
index = index_cls([1, 1, 2, 3])
263266
assert index.is_monotonic_increasing is True
@@ -303,7 +306,7 @@ def test_cant_or_shouldnt_cast(self, dtype):
303306
# can't
304307
data = ["foo", "bar", "baz"]
305308
with pytest.raises(ValueError, match=msg):
306-
self._index_cls(data, dtype=dtype)
309+
Index(data, dtype=dtype)
307310

308311
def test_view_index(self, simple_index):
309312
index = simple_index
@@ -315,27 +318,17 @@ def test_prevent_casting(self, simple_index):
315318
assert result.dtype == np.object_
316319

317320

318-
class TestIntNumericIndex(NumericInt):
321+
class TestIntNumericIndex:
319322
@pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8])
320323
def dtype(self, request):
321324
return request.param
322325

323-
@pytest.fixture
324-
def simple_index(self, dtype):
325-
return self._index_cls(range(0, 20, 2), dtype=dtype)
326-
327-
@pytest.fixture(
328-
params=[range(0, 20, 2), range(19, -1, -1)], ids=["index_inc", "index_dec"]
329-
)
330-
def index(self, request, dtype):
331-
return self._index_cls(request.param, dtype=dtype)
332-
333326
def test_constructor_from_list_no_dtype(self):
334-
index = self._index_cls([1, 2, 3])
327+
index = Index([1, 2, 3])
335328
assert index.dtype == np.int64
336329

337330
def test_constructor(self, dtype):
338-
index_cls = self._index_cls
331+
index_cls = Index
339332

340333
# scalar raise Exception
341334
msg = (
@@ -379,7 +372,7 @@ def test_constructor(self, dtype):
379372
tm.assert_index_equal(idx, expected)
380373

381374
def test_constructor_corner(self, dtype):
382-
index_cls = self._index_cls
375+
index_cls = Index
383376

384377
arr = np.array([1, 2, 3, 4], dtype=object)
385378

@@ -426,7 +419,7 @@ def test_constructor_np_unsigned(self, any_unsigned_int_numpy_dtype):
426419
def test_coerce_list(self):
427420
# coerce things
428421
arr = Index([1, 2, 3, 4])
429-
assert isinstance(arr, self._index_cls)
422+
assert isinstance(arr, Index)
430423

431424
# but not if explicit dtype passed
432425
arr = Index([1, 2, 3, 4], dtype=object)
@@ -436,10 +429,8 @@ def test_coerce_list(self):
436429
class TestFloat16Index:
437430
# float 16 indexes not supported
438431
# GH 49535
439-
_index_cls = Index
440-
441432
def test_constructor(self):
442-
index_cls = self._index_cls
433+
index_cls = Index
443434
dtype = np.float16
444435

445436
msg = "float16 indexes are not supported"
@@ -471,27 +462,6 @@ def test_constructor(self):
471462
index_cls(np.array([np.nan]), dtype=dtype)
472463

473464

474-
class TestUIntNumericIndex(NumericInt):
475-
@pytest.fixture(params=[np.uint64])
476-
def dtype(self, request):
477-
return request.param
478-
479-
@pytest.fixture
480-
def simple_index(self, dtype):
481-
# compat with shared Int64/Float64 tests
482-
return self._index_cls(np.arange(5, dtype=dtype))
483-
484-
@pytest.fixture(
485-
params=[
486-
[2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25],
487-
[2**63 + 25, 2**63 + 20, 2**63 + 15, 2**63 + 10, 2**63],
488-
],
489-
ids=["index_inc", "index_dec"],
490-
)
491-
def index(self, request):
492-
return self._index_cls(request.param, dtype=np.uint64)
493-
494-
495465
@pytest.mark.parametrize(
496466
"box",
497467
[list, lambda x: np.array(x, dtype=object), lambda x: Index(x, dtype=object)],

0 commit comments

Comments
 (0)