|
11 | 11 | date_range,
|
12 | 12 | )
|
13 | 13 | import pandas._testing as tm
|
14 |
| -from pandas.tests.arrays.categorical.common import TestCategorical |
15 | 14 |
|
16 | 15 |
|
17 |
| -class TestCategoricalOpsWithFactor(TestCategorical): |
| 16 | +class TestCategoricalOpsWithFactor: |
18 | 17 | def test_categories_none_comparisons(self):
|
19 | 18 | 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) |
21 | 20 |
|
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"] |
25 | 24 | tm.assert_categorical_equal(result, expected)
|
26 | 25 |
|
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"] |
29 | 28 | tm.assert_categorical_equal(result, expected)
|
30 | 29 |
|
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"] |
33 | 32 | tm.assert_categorical_equal(result, expected)
|
34 | 33 |
|
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"] |
37 | 36 | tm.assert_categorical_equal(result, expected)
|
38 | 37 |
|
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"] |
41 | 40 | tm.assert_categorical_equal(result, expected)
|
42 | 41 |
|
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"] |
45 | 44 | tm.assert_categorical_equal(result, expected)
|
46 | 45 |
|
47 |
| - n = len(self.factor) |
| 46 | + n = len(factor) |
48 | 47 |
|
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) |
52 | 51 | tm.assert_numpy_array_equal(result, expected)
|
53 | 52 |
|
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) |
56 | 55 | tm.assert_numpy_array_equal(result, expected)
|
57 | 56 |
|
58 | 57 | # comparisons with categoricals
|
@@ -377,23 +376,31 @@ def test_numeric_like_ops(self):
|
377 | 376 |
|
378 | 377 | # mad technically works because it takes always the numeric data
|
379 | 378 |
|
| 379 | + def test_numeric_like_ops_series(self): |
380 | 380 | # numpy ops
|
381 | 381 | s = Series(Categorical([1, 2, 3, 4]))
|
382 | 382 | with pytest.raises(TypeError, match="does not support reduction 'sum'"):
|
383 | 383 | np.sum(s)
|
384 | 384 |
|
385 |
| - # numeric ops on a Series |
386 |
| - for op, str_rep in [ |
| 385 | + @pytest.mark.parametrize( |
| 386 | + "op, str_rep", |
| 387 | + [ |
387 | 388 | ("__add__", r"\+"),
|
388 | 389 | ("__sub__", "-"),
|
389 | 390 | ("__mul__", r"\*"),
|
390 | 391 | ("__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) |
395 | 400 |
|
| 401 | + def test_numeric_like_ops_series_invalid(self): |
396 | 402 | # invalid ufunc
|
| 403 | + s = Series(Categorical([1, 2, 3, 4])) |
397 | 404 | msg = "Object with dtype category cannot perform the numpy op log"
|
398 | 405 | with pytest.raises(TypeError, match=msg):
|
399 | 406 | np.log(s)
|
0 commit comments