|
6 | 6 |
|
7 | 7 |
|
8 | 8 | @pytest.mark.parametrize(
|
9 |
| - "to_replace,value,expected,flip_categories", |
| 9 | + "to_replace,value,expected", |
10 | 10 | [
|
11 | 11 | # one-to-one
|
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), |
| 12 | + (4, 1, [1, 2, 3]), |
| 13 | + (3, 1, [1, 2, 1]), |
16 | 14 | # many-to-one
|
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), |
22 |
| - ([1], [2], [2, 2, 3], False), |
23 |
| - ([1, 4], [5, 2], [5, 2, 3], False), |
24 |
| - # GH49404: overlap between to_replace and value |
25 |
| - ([1, 2, 3], [2, 3, 4], [2, 3, 4], False), |
26 |
| - # GH50872, GH46884: replace with null |
27 |
| - (1, None, [None, 2, 3], False), |
28 |
| - (1, pd.NA, [None, 2, 3], False), |
29 |
| - # check_categorical sorts categories, which crashes on mixed dtypes |
30 |
| - (3, "4", [1, 2, "4"], False), |
31 |
| - ([1, 2, "3"], "5", ["5", "5", 3], True), |
| 15 | + ((5, 6), 2, [1, 2, 3]), |
| 16 | + ((3, 2), 1, [1, 1, 1]), |
32 | 17 | ],
|
33 | 18 | )
|
34 |
| -@pytest.mark.filterwarnings( |
35 |
| - "ignore:.*with CategoricalDtype is deprecated:FutureWarning" |
36 |
| -) |
37 |
| -def test_replace_categorical_series(to_replace, value, expected, flip_categories): |
| 19 | +def test_replace_categorical_series(to_replace, value, expected): |
38 | 20 | # GH 31720
|
39 |
| - |
40 | 21 | ser = pd.Series([1, 2, 3], dtype="category")
|
41 | 22 | result = ser.replace(to_replace, value)
|
42 |
| - expected = pd.Series(expected, dtype="category") |
43 |
| - ser.replace(to_replace, value, inplace=True) |
44 |
| - |
45 |
| - if flip_categories: |
46 |
| - expected = expected.cat.set_categories(expected.cat.categories[::-1]) |
47 |
| - |
48 |
| - tm.assert_series_equal(expected, result, check_category_order=False) |
49 |
| - tm.assert_series_equal(expected, ser, check_category_order=False) |
| 23 | + expected = pd.Series(Categorical(expected, categories=[1, 2, 3])) |
| 24 | + tm.assert_series_equal(result, expected) |
50 | 25 |
|
51 | 26 |
|
52 | 27 | @pytest.mark.parametrize(
|
53 |
| - "to_replace, value, result, expected_error_msg", |
| 28 | + "to_replace,value", |
54 | 29 | [
|
55 |
| - ("b", "c", ["a", "c"], "Categorical.categories are different"), |
56 |
| - ("c", "d", ["a", "b"], None), |
57 |
| - # https://github.com/pandas-dev/pandas/issues/33288 |
58 |
| - ("a", "a", ["a", "b"], None), |
59 |
| - ("b", None, ["a", None], "Categorical.categories length are different"), |
| 30 | + # one-to-one |
| 31 | + (3, 5), |
| 32 | + # many-to-one |
| 33 | + ((3, 2), 5), |
60 | 34 | ],
|
61 | 35 | )
|
62 |
| -def test_replace_categorical(to_replace, value, result, expected_error_msg): |
63 |
| - # GH#26988 |
64 |
| - cat = Categorical(["a", "b"]) |
65 |
| - expected = Categorical(result) |
66 |
| - msg = ( |
67 |
| - r"The behavior of Series\.replace \(and DataFrame.replace\) " |
68 |
| - "with CategoricalDtype" |
69 |
| - ) |
70 |
| - warn = FutureWarning if expected_error_msg is not None else None |
71 |
| - with tm.assert_produces_warning(warn, match=msg): |
72 |
| - result = pd.Series(cat, copy=False).replace(to_replace, value)._values |
| 36 | +def test_replace_categorical_series_new_category_raises(to_replace, value): |
| 37 | + # GH 31720 |
| 38 | + ser = pd.Series([1, 2, 3], dtype="category") |
| 39 | + with pytest.raises( |
| 40 | + TypeError, match="Cannot setitem on a Categorical with a new category" |
| 41 | + ): |
| 42 | + ser.replace(to_replace, value) |
73 | 43 |
|
74 |
| - tm.assert_categorical_equal(result, expected) |
75 |
| - if to_replace == "b": # the "c" test is supposed to be unchanged |
76 |
| - with pytest.raises(AssertionError, match=expected_error_msg): |
77 |
| - # ensure non-inplace call does not affect original |
78 |
| - tm.assert_categorical_equal(cat, expected) |
79 | 44 |
|
80 |
| - ser = pd.Series(cat, copy=False) |
81 |
| - with tm.assert_produces_warning(warn, match=msg): |
82 |
| - ser.replace(to_replace, value, inplace=True) |
83 |
| - tm.assert_categorical_equal(cat, expected) |
| 45 | +def test_replace_maintain_ordering(): |
| 46 | + # GH51016 |
| 47 | + dtype = pd.CategoricalDtype([0, 1, 2], ordered=True) |
| 48 | + ser = pd.Series([0, 1, 2], dtype=dtype) |
| 49 | + result = ser.replace(0, 2) |
| 50 | + expected = pd.Series([2, 1, 2], dtype=dtype) |
| 51 | + tm.assert_series_equal(expected, result, check_category_order=True) |
84 | 52 |
|
85 | 53 |
|
86 | 54 | def test_replace_categorical_ea_dtype():
|
87 | 55 | # GH49404
|
88 |
| - cat = Categorical(pd.array(["a", "b"], dtype="string")) |
89 |
| - msg = ( |
90 |
| - r"The behavior of Series\.replace \(and DataFrame.replace\) " |
91 |
| - "with CategoricalDtype" |
| 56 | + cat = Categorical(pd.array(["a", "b", "c"], dtype="string")) |
| 57 | + result = pd.Series(cat).replace(["a", "b"], ["c", "c"])._values |
| 58 | + expected = Categorical( |
| 59 | + pd.array(["c"] * 3, dtype="string"), |
| 60 | + categories=pd.array(["a", "b", "c"], dtype="string"), |
92 | 61 | )
|
93 |
| - with tm.assert_produces_warning(FutureWarning, match=msg): |
94 |
| - result = pd.Series(cat).replace(["a", "b"], ["c", pd.NA])._values |
95 |
| - expected = Categorical(pd.array(["c", pd.NA], dtype="string")) |
96 | 62 | tm.assert_categorical_equal(result, expected)
|
97 | 63 |
|
98 | 64 |
|
99 |
| -def test_replace_maintain_ordering(): |
100 |
| - # GH51016 |
101 |
| - dtype = pd.CategoricalDtype([0, 1, 2], ordered=True) |
102 |
| - ser = pd.Series([0, 1, 2], dtype=dtype) |
103 |
| - msg = ( |
104 |
| - r"The behavior of Series\.replace \(and DataFrame.replace\) " |
105 |
| - "with CategoricalDtype" |
106 |
| - ) |
107 |
| - with tm.assert_produces_warning(FutureWarning, match=msg): |
108 |
| - result = ser.replace(0, 2) |
109 |
| - expected_dtype = pd.CategoricalDtype([1, 2], ordered=True) |
110 |
| - expected = pd.Series([2, 1, 2], dtype=expected_dtype) |
111 |
| - tm.assert_series_equal(expected, result, check_category_order=True) |
| 65 | +def test_replace_categorical_ea_dtype_different_cats_raises(): |
| 66 | + # GH49404 |
| 67 | + cat = Categorical(pd.array(["a", "b"], dtype="string")) |
| 68 | + with pytest.raises( |
| 69 | + TypeError, match="Cannot setitem on a Categorical with a new category" |
| 70 | + ): |
| 71 | + pd.Series(cat).replace(["a", "b"], ["c", pd.NA]) |
0 commit comments