Skip to content

Commit c26ac5d

Browse files
authored
TST/REF: take things out of Base tests (#38072)
1 parent 14e0d12 commit c26ac5d

File tree

6 files changed

+140
-149
lines changed

6 files changed

+140
-149
lines changed

pandas/tests/indexes/categorical/test_category.py

+90-86
Original file line numberDiff line numberDiff line change
@@ -28,81 +28,6 @@ def test_can_hold_identifiers(self):
2828
key = idx[0]
2929
assert idx._can_hold_identifiers_and_holds_name(key) is True
3030

31-
@pytest.mark.parametrize(
32-
"func,op_name",
33-
[
34-
(lambda idx: idx - idx, "__sub__"),
35-
(lambda idx: idx + idx, "__add__"),
36-
(lambda idx: idx - ["a", "b"], "__sub__"),
37-
(lambda idx: idx + ["a", "b"], "__add__"),
38-
(lambda idx: ["a", "b"] - idx, "__rsub__"),
39-
(lambda idx: ["a", "b"] + idx, "__radd__"),
40-
],
41-
)
42-
def test_disallow_addsub_ops(self, func, op_name):
43-
# GH 10039
44-
# set ops (+/-) raise TypeError
45-
idx = Index(Categorical(["a", "b"]))
46-
cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
47-
msg = "|".join(
48-
[
49-
f"cannot perform {op_name} with this index type: CategoricalIndex",
50-
"can only concatenate list",
51-
rf"unsupported operand type\(s\) for [\+-]: {cat_or_list}",
52-
]
53-
)
54-
with pytest.raises(TypeError, match=msg):
55-
func(idx)
56-
57-
def test_method_delegation(self):
58-
59-
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
60-
result = ci.set_categories(list("cab"))
61-
tm.assert_index_equal(
62-
result, CategoricalIndex(list("aabbca"), categories=list("cab"))
63-
)
64-
65-
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
66-
result = ci.rename_categories(list("efg"))
67-
tm.assert_index_equal(
68-
result, CategoricalIndex(list("ffggef"), categories=list("efg"))
69-
)
70-
71-
# GH18862 (let rename_categories take callables)
72-
result = ci.rename_categories(lambda x: x.upper())
73-
tm.assert_index_equal(
74-
result, CategoricalIndex(list("AABBCA"), categories=list("CAB"))
75-
)
76-
77-
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
78-
result = ci.add_categories(["d"])
79-
tm.assert_index_equal(
80-
result, CategoricalIndex(list("aabbca"), categories=list("cabd"))
81-
)
82-
83-
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
84-
result = ci.remove_categories(["c"])
85-
tm.assert_index_equal(
86-
result,
87-
CategoricalIndex(list("aabb") + [np.nan] + ["a"], categories=list("ab")),
88-
)
89-
90-
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
91-
result = ci.as_unordered()
92-
tm.assert_index_equal(result, ci)
93-
94-
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
95-
result = ci.as_ordered()
96-
tm.assert_index_equal(
97-
result,
98-
CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=True),
99-
)
100-
101-
# invalid
102-
msg = "cannot use inplace with CategoricalIndex"
103-
with pytest.raises(ValueError, match=msg):
104-
ci.set_categories(list("cab"), inplace=True)
105-
10631
def test_append(self):
10732

10833
ci = self.create_index()
@@ -387,6 +312,24 @@ def test_frame_repr(self):
387312
expected = " A\na 1\nb 2\nc 3"
388313
assert result == expected
389314

315+
def test_reindex_base(self):
316+
# See test_reindex.py
317+
pass
318+
319+
def test_map_str(self):
320+
# See test_map.py
321+
pass
322+
323+
324+
class TestCategoricalIndex2:
325+
# Tests that are not overriding a test in Base
326+
327+
def test_format_different_scalar_lengths(self):
328+
# GH35439
329+
idx = CategoricalIndex(["aaaaaaaaa", "b"])
330+
expected = ["aaaaaaaaa", "b"]
331+
assert idx.format() == expected
332+
390333
@pytest.mark.parametrize(
391334
"dtype, engine_type",
392335
[
@@ -410,16 +353,77 @@ def test_engine_type(self, dtype, engine_type):
410353
assert np.issubdtype(ci.codes.dtype, dtype)
411354
assert isinstance(ci._engine, engine_type)
412355

413-
def test_reindex_base(self):
414-
# See test_reindex.py
415-
pass
356+
@pytest.mark.parametrize(
357+
"func,op_name",
358+
[
359+
(lambda idx: idx - idx, "__sub__"),
360+
(lambda idx: idx + idx, "__add__"),
361+
(lambda idx: idx - ["a", "b"], "__sub__"),
362+
(lambda idx: idx + ["a", "b"], "__add__"),
363+
(lambda idx: ["a", "b"] - idx, "__rsub__"),
364+
(lambda idx: ["a", "b"] + idx, "__radd__"),
365+
],
366+
)
367+
def test_disallow_addsub_ops(self, func, op_name):
368+
# GH 10039
369+
# set ops (+/-) raise TypeError
370+
idx = Index(Categorical(["a", "b"]))
371+
cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
372+
msg = "|".join(
373+
[
374+
f"cannot perform {op_name} with this index type: CategoricalIndex",
375+
"can only concatenate list",
376+
rf"unsupported operand type\(s\) for [\+-]: {cat_or_list}",
377+
]
378+
)
379+
with pytest.raises(TypeError, match=msg):
380+
func(idx)
416381

417-
def test_map_str(self):
418-
# See test_map.py
419-
pass
382+
def test_method_delegation(self):
420383

421-
def test_format_different_scalar_lengths(self):
422-
# GH35439
423-
idx = CategoricalIndex(["aaaaaaaaa", "b"])
424-
expected = ["aaaaaaaaa", "b"]
425-
assert idx.format() == expected
384+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
385+
result = ci.set_categories(list("cab"))
386+
tm.assert_index_equal(
387+
result, CategoricalIndex(list("aabbca"), categories=list("cab"))
388+
)
389+
390+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
391+
result = ci.rename_categories(list("efg"))
392+
tm.assert_index_equal(
393+
result, CategoricalIndex(list("ffggef"), categories=list("efg"))
394+
)
395+
396+
# GH18862 (let rename_categories take callables)
397+
result = ci.rename_categories(lambda x: x.upper())
398+
tm.assert_index_equal(
399+
result, CategoricalIndex(list("AABBCA"), categories=list("CAB"))
400+
)
401+
402+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
403+
result = ci.add_categories(["d"])
404+
tm.assert_index_equal(
405+
result, CategoricalIndex(list("aabbca"), categories=list("cabd"))
406+
)
407+
408+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
409+
result = ci.remove_categories(["c"])
410+
tm.assert_index_equal(
411+
result,
412+
CategoricalIndex(list("aabb") + [np.nan] + ["a"], categories=list("ab")),
413+
)
414+
415+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
416+
result = ci.as_unordered()
417+
tm.assert_index_equal(result, ci)
418+
419+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
420+
result = ci.as_ordered()
421+
tm.assert_index_equal(
422+
result,
423+
CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=True),
424+
)
425+
426+
# invalid
427+
msg = "cannot use inplace with CategoricalIndex"
428+
with pytest.raises(ValueError, match=msg):
429+
ci.set_categories(list("cab"), inplace=True)

pandas/tests/indexes/interval/test_astype.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pandas._testing as tm
1616

1717

18-
class Base:
18+
class AstypeTests:
1919
"""Tests common to IntervalIndex with any subtype"""
2020

2121
def test_astype_idempotent(self, index):
@@ -72,7 +72,7 @@ def test_astype_invalid_dtype(self, index):
7272
index.astype("fake_dtype")
7373

7474

75-
class TestIntSubtype(Base):
75+
class TestIntSubtype(AstypeTests):
7676
"""Tests specific to IntervalIndex with integer-like subtype"""
7777

7878
indexes = [
@@ -124,7 +124,7 @@ def test_subtype_integer_errors(self):
124124
index.astype(dtype)
125125

126126

127-
class TestFloatSubtype(Base):
127+
class TestFloatSubtype(AstypeTests):
128128
"""Tests specific to IntervalIndex with float subtype"""
129129

130130
indexes = [
@@ -179,7 +179,7 @@ def test_subtype_datetimelike(self, index, subtype):
179179
index.astype(dtype)
180180

181181

182-
class TestDatetimelikeSubtype(Base):
182+
class TestDatetimelikeSubtype(AstypeTests):
183183
"""Tests specific to IntervalIndex with datetime-like subtype"""
184184

185185
indexes = [

pandas/tests/indexes/interval/test_base.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def test_where(self, closed, klass):
5252
result = idx.where(klass(cond))
5353
tm.assert_index_equal(result, expected)
5454

55+
def test_getitem_2d_deprecated(self):
56+
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
57+
idx = self.create_index()
58+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
59+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
60+
idx[:, None]
61+
62+
63+
class TestPutmask:
5564
@pytest.mark.parametrize("tz", ["US/Pacific", None])
5665
def test_putmask_dt64(self, tz):
5766
# GH#37968
@@ -75,10 +84,3 @@ def test_putmask_td64(self):
7584
result = idx.putmask(mask, idx[-1])
7685
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
7786
tm.assert_index_equal(result, expected)
78-
79-
def test_getitem_2d_deprecated(self):
80-
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
81-
idx = self.create_index()
82-
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
83-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
84-
idx[:, None]

pandas/tests/indexes/interval/test_constructors.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def name(request):
2929
return request.param
3030

3131

32-
class Base:
32+
class ConstructorTests:
3333
"""
3434
Common tests for all variations of IntervalIndex construction. Input data
3535
to be supplied in breaks format, then converted by the subclass method
@@ -182,7 +182,7 @@ def test_generic_errors(self, constructor):
182182
constructor(**decreasing_kwargs)
183183

184184

185-
class TestFromArrays(Base):
185+
class TestFromArrays(ConstructorTests):
186186
"""Tests specific to IntervalIndex.from_arrays"""
187187

188188
@pytest.fixture
@@ -231,7 +231,7 @@ def test_mixed_float_int(self, left_subtype, right_subtype):
231231
assert result.dtype.subtype == expected_subtype
232232

233233

234-
class TestFromBreaks(Base):
234+
class TestFromBreaks(ConstructorTests):
235235
"""Tests specific to IntervalIndex.from_breaks"""
236236

237237
@pytest.fixture
@@ -269,7 +269,7 @@ def test_left_right_dont_share_data(self):
269269
assert result._left.base is None or result._left.base is not result._right.base
270270

271271

272-
class TestFromTuples(Base):
272+
class TestFromTuples(ConstructorTests):
273273
"""Tests specific to IntervalIndex.from_tuples"""
274274

275275
@pytest.fixture
@@ -316,7 +316,7 @@ def test_na_tuples(self):
316316
tm.assert_index_equal(idx_na_tuple, idx_na_element)
317317

318318

319-
class TestClassConstructors(Base):
319+
class TestClassConstructors(ConstructorTests):
320320
"""Tests specific to the IntervalIndex/Index constructors"""
321321

322322
@pytest.fixture(

pandas/tests/indexes/ranges/test_range.py

-25
Original file line numberDiff line numberDiff line change
@@ -318,31 +318,6 @@ def test_slice_keep_name(self):
318318
idx = RangeIndex(1, 2, name="asdf")
319319
assert idx.name == idx[1:].name
320320

321-
def test_explicit_conversions(self):
322-
323-
# GH 8608
324-
# add/sub are overridden explicitly for Float/Int Index
325-
idx = RangeIndex(5)
326-
327-
# float conversions
328-
arr = np.arange(5, dtype="int64") * 3.2
329-
expected = Float64Index(arr)
330-
fidx = idx * 3.2
331-
tm.assert_index_equal(fidx, expected)
332-
fidx = 3.2 * idx
333-
tm.assert_index_equal(fidx, expected)
334-
335-
# interops with numpy arrays
336-
expected = Float64Index(arr)
337-
a = np.zeros(5, dtype="float64")
338-
result = fidx - a
339-
tm.assert_index_equal(result, expected)
340-
341-
expected = Float64Index(-arr)
342-
a = np.zeros(5, dtype="float64")
343-
result = a - fidx
344-
tm.assert_index_equal(result, expected)
345-
346321
def test_has_duplicates(self, index):
347322
assert index.is_unique
348323
assert not index.has_duplicates

0 commit comments

Comments
 (0)