|
| 1 | +import numpy as np |
1 | 2 | import pytest
|
2 | 3 |
|
3 | 4 | import pandas as pd
|
4 | 5 | import pandas._testing as tm
|
5 | 6 |
|
6 | 7 |
|
7 | 8 | @pytest.mark.parametrize(
|
8 |
| - "to_replace,value,expected,check_types,check_categorical", |
| 9 | + "to_replace,value,expected,flip_categories", |
9 | 10 | [
|
10 | 11 | # one-to-one
|
11 |
| - (1, 2, [2, 2, 3], True, True), |
12 |
| - (1, 4, [4, 2, 3], True, True), |
13 |
| - (4, 1, [1, 2, 3], True, True), |
14 |
| - (5, 6, [1, 2, 3], True, True), |
| 12 | + (1, 2, [2, 2, 3], False), |
| 13 | + (1, 4, [4, 2, 3], False), |
| 14 | + (4, 1, [1, 2, 3], False), |
| 15 | + (5, 6, [1, 2, 3], False), |
15 | 16 | # many-to-one
|
16 |
| - ([1], 2, [2, 2, 3], True, True), |
17 |
| - ([1, 2], 3, [3, 3, 3], True, True), |
18 |
| - ([1, 2], 4, [4, 4, 3], True, True), |
19 |
| - ((1, 2, 4), 5, [5, 5, 3], True, True), |
20 |
| - ((5, 6), 2, [1, 2, 3], True, True), |
| 17 | + ([1], 2, [2, 2, 3], False), |
| 18 | + ([1, 2], 3, [3, 3, 3], False), |
| 19 | + ([1, 2], 4, [4, 4, 3], False), |
| 20 | + ((1, 2, 4), 5, [5, 5, 3], False), |
| 21 | + ((5, 6), 2, [1, 2, 3], False), |
21 | 22 | # many-to-many, handled outside of Categorical and results in separate dtype
|
22 |
| - ([1], [2], [2, 2, 3], False, False), |
23 |
| - ([1, 4], [5, 2], [5, 2, 3], False, False), |
| 23 | + ([1], [2], [2, 2, 3], True), |
| 24 | + ([1, 4], [5, 2], [5, 2, 3], True), |
24 | 25 | # check_categorical sorts categories, which crashes on mixed dtypes
|
25 |
| - (3, "4", [1, 2, "4"], True, False), |
26 |
| - ([1, 2, "3"], "5", ["5", "5", 3], True, False), |
| 26 | + (3, "4", [1, 2, "4"], False), |
| 27 | + ([1, 2, "3"], "5", ["5", "5", 3], True), |
27 | 28 | ],
|
28 | 29 | )
|
29 |
| -def test_replace(to_replace, value, expected, check_types, check_categorical): |
| 30 | +def test_replace(to_replace, value, expected, flip_categories): |
30 | 31 | # GH 31720
|
| 32 | + stays_categorical = not isinstance(value, list) |
| 33 | + |
31 | 34 | s = pd.Series([1, 2, 3], dtype="category")
|
32 | 35 | result = s.replace(to_replace, value)
|
33 | 36 | expected = pd.Series(expected, dtype="category")
|
34 | 37 | s.replace(to_replace, value, inplace=True)
|
| 38 | + |
| 39 | + if flip_categories: |
| 40 | + expected = expected.cat.set_categories(expected.cat.categories[::-1]) |
| 41 | + |
| 42 | + if not stays_categorical: |
| 43 | + # the replace call loses categorical dtype |
| 44 | + expected = pd.Series(np.asarray(expected)) |
| 45 | + |
35 | 46 | tm.assert_series_equal(
|
36 |
| - expected, |
37 |
| - result, |
38 |
| - check_dtype=check_types, |
39 |
| - check_categorical=check_categorical, |
40 |
| - check_category_order=False, |
| 47 | + expected, result, check_category_order=False, |
41 | 48 | )
|
42 | 49 | tm.assert_series_equal(
|
43 |
| - expected, |
44 |
| - s, |
45 |
| - check_dtype=check_types, |
46 |
| - check_categorical=check_categorical, |
47 |
| - check_category_order=False, |
| 50 | + expected, s, check_category_order=False, |
48 | 51 | )
|
0 commit comments