Skip to content

Commit c9eda43

Browse files
authored
TST: Use fixture instead of base class setup (#45988)
1 parent cff780f commit c9eda43

File tree

9 files changed

+72
-76
lines changed

9 files changed

+72
-76
lines changed

pandas/tests/arrays/categorical/common.py

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import pytest
22

3+
from pandas import Categorical
4+
35

46
@pytest.fixture(params=[True, False])
57
def allow_fill(request):
68
"""Boolean 'allow_fill' parameter for Categorical.take"""
79
return request.param
10+
11+
12+
@pytest.fixture
13+
def factor():
14+
return Categorical(["a", "b", "b", "a", "a", "c", "c", "c"], ordered=True)

pandas/tests/arrays/categorical/test_api.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
)
1313
import pandas._testing as tm
1414
from pandas.core.arrays.categorical import recode_for_categories
15-
from pandas.tests.arrays.categorical.common import TestCategorical
1615

1716

1817
class TestCategoricalAPI:
@@ -427,21 +426,21 @@ def test_remove_unused_categories(self):
427426
assert out.tolist() == val.tolist()
428427

429428

430-
class TestCategoricalAPIWithFactor(TestCategorical):
431-
def test_describe(self):
429+
class TestCategoricalAPIWithFactor:
430+
def test_describe(self, factor):
432431
# string type
433-
desc = self.factor.describe()
434-
assert self.factor.ordered
432+
desc = factor.describe()
433+
assert factor.ordered
435434
exp_index = CategoricalIndex(
436-
["a", "b", "c"], name="categories", ordered=self.factor.ordered
435+
["a", "b", "c"], name="categories", ordered=factor.ordered
437436
)
438437
expected = DataFrame(
439438
{"counts": [3, 2, 3], "freqs": [3 / 8.0, 2 / 8.0, 3 / 8.0]}, index=exp_index
440439
)
441440
tm.assert_frame_equal(desc, expected)
442441

443442
# check unused categories
444-
cat = self.factor.copy()
443+
cat = factor.copy()
445444

446445
with tm.assert_produces_warning(FutureWarning):
447446
# issue #37643 inplace kwarg deprecated
@@ -450,7 +449,7 @@ def test_describe(self):
450449
desc = cat.describe()
451450

452451
exp_index = CategoricalIndex(
453-
list("abcd"), ordered=self.factor.ordered, name="categories"
452+
list("abcd"), ordered=factor.ordered, name="categories"
454453
)
455454
expected = DataFrame(
456455
{"counts": [3, 2, 3, 0], "freqs": [3 / 8.0, 2 / 8.0, 3 / 8.0, 0]},
@@ -480,8 +479,8 @@ def test_describe(self):
480479
)
481480
tm.assert_frame_equal(desc, expected)
482481

483-
def test_set_categories_inplace(self):
484-
cat = self.factor.copy()
482+
def test_set_categories_inplace(self, factor):
483+
cat = factor.copy()
485484

486485
with tm.assert_produces_warning(FutureWarning):
487486
# issue #37643 inplace kwarg deprecated

pandas/tests/arrays/categorical/test_indexing.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,30 @@
1414
)
1515
import pandas._testing as tm
1616
import pandas.core.common as com
17-
from pandas.tests.arrays.categorical.common import TestCategorical
1817

1918

20-
class TestCategoricalIndexingWithFactor(TestCategorical):
21-
def test_getitem(self):
22-
assert self.factor[0] == "a"
23-
assert self.factor[-1] == "c"
19+
class TestCategoricalIndexingWithFactor:
20+
def test_getitem(self, factor):
21+
assert factor[0] == "a"
22+
assert factor[-1] == "c"
2423

25-
subf = self.factor[[0, 1, 2]]
24+
subf = factor[[0, 1, 2]]
2625
tm.assert_numpy_array_equal(subf._codes, np.array([0, 1, 1], dtype=np.int8))
2726

28-
subf = self.factor[np.asarray(self.factor) == "c"]
27+
subf = factor[np.asarray(factor) == "c"]
2928
tm.assert_numpy_array_equal(subf._codes, np.array([2, 2, 2], dtype=np.int8))
3029

31-
def test_setitem(self):
30+
def test_setitem(self, factor):
3231

3332
# int/positional
34-
c = self.factor.copy()
33+
c = factor.copy()
3534
c[0] = "b"
3635
assert c[0] == "b"
3736
c[-1] = "a"
3837
assert c[-1] == "a"
3938

4039
# boolean
41-
c = self.factor.copy()
40+
c = factor.copy()
4241
indexer = np.zeros(len(c), dtype="bool")
4342
indexer[0] = True
4443
indexer[-1] = True

pandas/tests/arrays/categorical/test_operators.py

+35-28
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,47 @@
1111
date_range,
1212
)
1313
import pandas._testing as tm
14-
from pandas.tests.arrays.categorical.common import TestCategorical
1514

1615

17-
class TestCategoricalOpsWithFactor(TestCategorical):
16+
class TestCategoricalOpsWithFactor:
1817
def test_categories_none_comparisons(self):
1918
factor = Categorical(["a", "b", "b", "a", "a", "c", "c", "c"], ordered=True)
20-
tm.assert_categorical_equal(factor, self.factor)
19+
tm.assert_categorical_equal(factor, factor)
2120

22-
def test_comparisons(self):
23-
result = self.factor[self.factor == "a"]
24-
expected = self.factor[np.asarray(self.factor) == "a"]
21+
def test_comparisons(self, factor):
22+
result = factor[factor == "a"]
23+
expected = factor[np.asarray(factor) == "a"]
2524
tm.assert_categorical_equal(result, expected)
2625

27-
result = self.factor[self.factor != "a"]
28-
expected = self.factor[np.asarray(self.factor) != "a"]
26+
result = factor[factor != "a"]
27+
expected = factor[np.asarray(factor) != "a"]
2928
tm.assert_categorical_equal(result, expected)
3029

31-
result = self.factor[self.factor < "c"]
32-
expected = self.factor[np.asarray(self.factor) < "c"]
30+
result = factor[factor < "c"]
31+
expected = factor[np.asarray(factor) < "c"]
3332
tm.assert_categorical_equal(result, expected)
3433

35-
result = self.factor[self.factor > "a"]
36-
expected = self.factor[np.asarray(self.factor) > "a"]
34+
result = factor[factor > "a"]
35+
expected = factor[np.asarray(factor) > "a"]
3736
tm.assert_categorical_equal(result, expected)
3837

39-
result = self.factor[self.factor >= "b"]
40-
expected = self.factor[np.asarray(self.factor) >= "b"]
38+
result = factor[factor >= "b"]
39+
expected = factor[np.asarray(factor) >= "b"]
4140
tm.assert_categorical_equal(result, expected)
4241

43-
result = self.factor[self.factor <= "b"]
44-
expected = self.factor[np.asarray(self.factor) <= "b"]
42+
result = factor[factor <= "b"]
43+
expected = factor[np.asarray(factor) <= "b"]
4544
tm.assert_categorical_equal(result, expected)
4645

47-
n = len(self.factor)
46+
n = len(factor)
4847

49-
other = self.factor[np.random.permutation(n)]
50-
result = self.factor == other
51-
expected = np.asarray(self.factor) == np.asarray(other)
48+
other = factor[np.random.permutation(n)]
49+
result = factor == other
50+
expected = np.asarray(factor) == np.asarray(other)
5251
tm.assert_numpy_array_equal(result, expected)
5352

54-
result = self.factor == "d"
55-
expected = np.zeros(len(self.factor), dtype=bool)
53+
result = factor == "d"
54+
expected = np.zeros(len(factor), dtype=bool)
5655
tm.assert_numpy_array_equal(result, expected)
5756

5857
# comparisons with categoricals
@@ -377,23 +376,31 @@ def test_numeric_like_ops(self):
377376

378377
# mad technically works because it takes always the numeric data
379378

379+
def test_numeric_like_ops_series(self):
380380
# numpy ops
381381
s = Series(Categorical([1, 2, 3, 4]))
382382
with pytest.raises(TypeError, match="does not support reduction 'sum'"):
383383
np.sum(s)
384384

385-
# numeric ops on a Series
386-
for op, str_rep in [
385+
@pytest.mark.parametrize(
386+
"op, str_rep",
387+
[
387388
("__add__", r"\+"),
388389
("__sub__", "-"),
389390
("__mul__", r"\*"),
390391
("__truediv__", "/"),
391-
]:
392-
msg = f"Series cannot perform the operation {str_rep}|unsupported operand"
393-
with pytest.raises(TypeError, match=msg):
394-
getattr(s, op)(2)
392+
],
393+
)
394+
def test_numeric_like_ops_series_arith(self, op, str_rep):
395+
# numeric ops on a Series
396+
s = Series(Categorical([1, 2, 3, 4]))
397+
msg = f"Series cannot perform the operation {str_rep}|unsupported operand"
398+
with pytest.raises(TypeError, match=msg):
399+
getattr(s, op)(2)
395400

401+
def test_numeric_like_ops_series_invalid(self):
396402
# invalid ufunc
403+
s = Series(Categorical([1, 2, 3, 4]))
397404
msg = "Object with dtype category cannot perform the numpy op log"
398405
with pytest.raises(TypeError, match=msg):
399406
np.log(s)

pandas/tests/arrays/categorical/test_repr.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
period_range,
1010
timedelta_range,
1111
)
12-
from pandas.tests.arrays.categorical.common import TestCategorical
1312

1413

15-
class TestCategoricalReprWithFactor(TestCategorical):
16-
def test_print(self):
14+
class TestCategoricalReprWithFactor:
15+
def test_print(self, factor):
1716
expected = [
1817
"['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c']",
1918
"Categories (3, object): ['a' < 'b' < 'c']",
2019
]
2120
expected = "\n".join(expected)
22-
actual = repr(self.factor)
21+
actual = repr(factor)
2322
assert actual == expected
2423

2524

pandas/tests/arrays/sparse/test_arithmetics.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -500,23 +500,19 @@ def test_mismatched_length_cmp_op(cons):
500500

501501

502502
@pytest.mark.parametrize("op", ["add", "sub", "mul", "truediv", "floordiv", "pow"])
503-
def test_binary_operators(op):
503+
@pytest.mark.parametrize("fill_value", [np.nan, 3])
504+
def test_binary_operators(op, fill_value):
504505
op = getattr(operator, op)
505506
data1 = np.random.randn(20)
506507
data2 = np.random.randn(20)
507508

508-
data1[::2] = np.nan
509-
data2[::3] = np.nan
509+
data1[::2] = fill_value
510+
data2[::3] = fill_value
510511

511-
arr1 = SparseArray(data1)
512-
arr2 = SparseArray(data2)
512+
first = SparseArray(data1, fill_value=fill_value)
513+
second = SparseArray(data2, fill_value=fill_value)
513514

514-
data1[::2] = 3
515-
data2[::3] = 3
516-
farr1 = SparseArray(data1, fill_value=3)
517-
farr2 = SparseArray(data2, fill_value=3)
518-
519-
def _check_op(op, first, second):
515+
with np.errstate(all="ignore"):
520516
res = op(first, second)
521517
exp = SparseArray(
522518
op(first.to_dense(), second.to_dense()), fill_value=first.fill_value
@@ -544,7 +540,3 @@ def _check_op(op, first, second):
544540
else:
545541
tm.assert_almost_equal(res4.fill_value, exp_fv)
546542
tm.assert_almost_equal(res4.to_dense(), exp)
547-
548-
with np.errstate(all="ignore"):
549-
for first_arr, second_arr in [(arr1, arr2), (farr1, farr2)]:
550-
_check_op(op, first_arr, second_arr)

pandas/tests/arrays/sparse/test_constructors.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_constructor_object_dtype(self):
7171
assert arr.dtype == SparseDtype(object, "A")
7272
assert arr.fill_value == "A"
7373

74+
def test_constructor_object_dtype_bool_fill(self):
7475
# GH#17574
7576
data = [False, 0, 100.0, 0.0]
7677
arr = SparseArray(data, dtype=object, fill_value=False)

pandas/tests/arrays/sparse/test_unary.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_abs_operator(self):
6060

6161
def test_invert_operator(self):
6262
arr = SparseArray([False, True, False, True], fill_value=False, dtype=np.bool8)
63-
res = ~arr
6463
exp = SparseArray(
6564
np.invert([False, True, False, True]), fill_value=True, dtype=np.bool8
6665
)
@@ -70,3 +69,4 @@ def test_invert_operator(self):
7069
arr = SparseArray([0, 1, 0, 2, 3, 0], fill_value=0, dtype=np.int32)
7170
res = ~arr
7271
exp = SparseArray([-1, -2, -1, -3, -4, -1], fill_value=-1, dtype=np.int32)
72+
tm.assert_sp_array_equal(exp, res)

0 commit comments

Comments
 (0)