|
9 | 9 | from pandas.tests.copy_view.util import get_array
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def test_replace_categorical_inplace_reference(using_copy_on_write): |
13 |
| - df = DataFrame({"a": Categorical([1, 2, 3])}) |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "replace_kwargs", |
| 14 | + [ |
| 15 | + {"to_replace": {"a": 1, "b": 4}, "value": -1}, |
| 16 | + # Test CoW splits blocks to avoid copying unchanged columns |
| 17 | + {"to_replace": {"a": 1}, "value": -1}, |
| 18 | + {"to_replace": {"b": 4}, "value": -1}, |
| 19 | + {"to_replace": {"b": {4: 1}}}, |
| 20 | + # TODO: Add these in a further optimization |
| 21 | + # We would need to see which columns got replaced in the mask |
| 22 | + # which could be expensive |
| 23 | + # {"to_replace": {"b": 1}}, |
| 24 | + # 1 |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_replace(using_copy_on_write, replace_kwargs): |
| 28 | + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": ["foo", "bar", "baz"]}) |
| 29 | + df_orig = df.copy() |
| 30 | + |
| 31 | + df_replaced = df.replace(**replace_kwargs) |
| 32 | + |
| 33 | + if using_copy_on_write: |
| 34 | + if (df_replaced["b"] == df["b"]).all(): |
| 35 | + assert np.shares_memory(get_array(df_replaced, "b"), get_array(df, "b")) |
| 36 | + assert np.shares_memory(get_array(df_replaced, "c"), get_array(df, "c")) |
| 37 | + |
| 38 | + # mutating squeezed df triggers a copy-on-write for that column/block |
| 39 | + df_replaced.loc[0, "c"] = -1 |
| 40 | + if using_copy_on_write: |
| 41 | + assert not np.shares_memory(get_array(df_replaced, "c"), get_array(df, "c")) |
| 42 | + |
| 43 | + if "a" in replace_kwargs["to_replace"]: |
| 44 | + arr = get_array(df_replaced, "a") |
| 45 | + df_replaced.loc[0, "a"] = 100 |
| 46 | + assert np.shares_memory(get_array(df_replaced, "a"), arr) |
| 47 | + tm.assert_frame_equal(df, df_orig) |
| 48 | + |
| 49 | + |
| 50 | +def test_replace_mask_all_false_second_block(using_copy_on_write): |
| 51 | + df = DataFrame({"a": [1.5, 2, 3], "b": 100.5, "c": 1, "d": 2}) |
| 52 | + df_orig = df.copy() |
| 53 | + |
| 54 | + df2 = df.replace(to_replace=1.5, value=55.5) |
| 55 | + |
| 56 | + if using_copy_on_write: |
| 57 | + # TODO: Block splitting would allow us to avoid copying b |
| 58 | + assert np.shares_memory(get_array(df, "c"), get_array(df2, "c")) |
| 59 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 60 | + |
| 61 | + else: |
| 62 | + assert not np.shares_memory(get_array(df, "c"), get_array(df2, "c")) |
| 63 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 64 | + |
| 65 | + df2.loc[0, "c"] = 1 |
| 66 | + tm.assert_frame_equal(df, df_orig) # Original is unchanged |
| 67 | + |
| 68 | + if using_copy_on_write: |
| 69 | + assert not np.shares_memory(get_array(df, "c"), get_array(df2, "c")) |
| 70 | + # TODO: This should split and not copy the whole block |
| 71 | + # assert np.shares_memory(get_array(df, "d"), get_array(df2, "d")) |
| 72 | + |
| 73 | + |
| 74 | +def test_replace_coerce_single_column(using_copy_on_write, using_array_manager): |
| 75 | + df = DataFrame({"a": [1.5, 2, 3], "b": 100.5}) |
14 | 76 | df_orig = df.copy()
|
| 77 | + |
| 78 | + df2 = df.replace(to_replace=1.5, value="a") |
| 79 | + |
| 80 | + if using_copy_on_write: |
| 81 | + assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 82 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 83 | + |
| 84 | + elif not using_array_manager: |
| 85 | + assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 86 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 87 | + |
| 88 | + if using_copy_on_write: |
| 89 | + df2.loc[0, "b"] = 0.5 |
| 90 | + tm.assert_frame_equal(df, df_orig) # Original is unchanged |
| 91 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 92 | + |
| 93 | + |
| 94 | +def test_replace_to_replace_wrong_dtype(using_copy_on_write): |
| 95 | + df = DataFrame({"a": [1.5, 2, 3], "b": 100.5}) |
| 96 | + df_orig = df.copy() |
| 97 | + |
| 98 | + df2 = df.replace(to_replace="xxx", value=1.5) |
| 99 | + |
| 100 | + if using_copy_on_write: |
| 101 | + assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 102 | + assert np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 103 | + |
| 104 | + else: |
| 105 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 106 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 107 | + |
| 108 | + df2.loc[0, "b"] = 0.5 |
| 109 | + tm.assert_frame_equal(df, df_orig) # Original is unchanged |
| 110 | + |
| 111 | + if using_copy_on_write: |
| 112 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 113 | + |
| 114 | + |
| 115 | +def test_replace_inplace(using_copy_on_write): |
| 116 | + df = DataFrame({"a": [1.5, 2, 3]}) |
| 117 | + arr_a = get_array(df, "a") |
| 118 | + df.replace(to_replace=1.5, value=15.5, inplace=True) |
| 119 | + |
| 120 | + assert np.shares_memory(get_array(df, "a"), arr_a) |
| 121 | + if using_copy_on_write: |
| 122 | + assert df._mgr._has_no_reference(0) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("to_replace", [1.5, [1.5]]) |
| 126 | +def test_replace_inplace_reference(using_copy_on_write, to_replace): |
| 127 | + df = DataFrame({"a": [1.5, 2, 3]}) |
15 | 128 | arr_a = get_array(df, "a")
|
16 | 129 | view = df[:]
|
17 |
| - df.replace(to_replace=[1], value=2, inplace=True) |
| 130 | + df.replace(to_replace=to_replace, value=15.5, inplace=True) |
18 | 131 |
|
19 | 132 | if using_copy_on_write:
|
20 |
| - assert not np.shares_memory(get_array(df, "a").codes, arr_a.codes) |
| 133 | + assert not np.shares_memory(get_array(df, "a"), arr_a) |
21 | 134 | assert df._mgr._has_no_reference(0)
|
22 | 135 | assert view._mgr._has_no_reference(0)
|
23 |
| - tm.assert_frame_equal(view, df_orig) |
24 | 136 | else:
|
25 |
| - assert np.shares_memory(get_array(df, "a").codes, arr_a.codes) |
| 137 | + assert np.shares_memory(get_array(df, "a"), arr_a) |
26 | 138 |
|
27 | 139 |
|
28 |
| -def test_replace_inplace_reference(using_copy_on_write): |
| 140 | +@pytest.mark.parametrize("to_replace", ["a", 100.5]) |
| 141 | +def test_replace_inplace_reference_no_op(using_copy_on_write, to_replace): |
29 | 142 | df = DataFrame({"a": [1.5, 2, 3]})
|
30 | 143 | arr_a = get_array(df, "a")
|
31 | 144 | view = df[:]
|
32 |
| - df.replace(to_replace=[1.5], value=15.5, inplace=True) |
| 145 | + df.replace(to_replace=to_replace, value=15.5, inplace=True) |
33 | 146 |
|
| 147 | + assert np.shares_memory(get_array(df, "a"), arr_a) |
34 | 148 | if using_copy_on_write:
|
35 |
| - assert not np.shares_memory(get_array(df, "a"), arr_a) |
| 149 | + assert not df._mgr._has_no_reference(0) |
| 150 | + assert not view._mgr._has_no_reference(0) |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize("to_replace", [1, [1]]) |
| 154 | +@pytest.mark.parametrize("val", [1, 1.5]) |
| 155 | +def test_replace_categorical_inplace_reference(using_copy_on_write, val, to_replace): |
| 156 | + df = DataFrame({"a": Categorical([1, 2, 3])}) |
| 157 | + df_orig = df.copy() |
| 158 | + arr_a = get_array(df, "a") |
| 159 | + view = df[:] |
| 160 | + df.replace(to_replace=to_replace, value=val, inplace=True) |
| 161 | + |
| 162 | + if using_copy_on_write: |
| 163 | + assert not np.shares_memory(get_array(df, "a").codes, arr_a.codes) |
36 | 164 | assert df._mgr._has_no_reference(0)
|
37 | 165 | assert view._mgr._has_no_reference(0)
|
| 166 | + tm.assert_frame_equal(view, df_orig) |
38 | 167 | else:
|
39 |
| - assert np.shares_memory(get_array(df, "a"), arr_a) |
| 168 | + assert np.shares_memory(get_array(df, "a").codes, arr_a.codes) |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.parametrize("val", [1, 1.5]) |
| 172 | +def test_replace_categorical_inplace(using_copy_on_write, val): |
| 173 | + df = DataFrame({"a": Categorical([1, 2, 3])}) |
| 174 | + arr_a = get_array(df, "a") |
| 175 | + df.replace(to_replace=1, value=val, inplace=True) |
| 176 | + |
| 177 | + assert np.shares_memory(get_array(df, "a").codes, arr_a.codes) |
| 178 | + if using_copy_on_write: |
| 179 | + assert df._mgr._has_no_reference(0) |
| 180 | + |
| 181 | + expected = DataFrame({"a": Categorical([val, 2, 3])}) |
| 182 | + tm.assert_frame_equal(df, expected) |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.parametrize("val", [1, 1.5]) |
| 186 | +def test_replace_categorical(using_copy_on_write, val): |
| 187 | + df = DataFrame({"a": Categorical([1, 2, 3])}) |
| 188 | + df_orig = df.copy() |
| 189 | + df2 = df.replace(to_replace=1, value=val) |
| 190 | + |
| 191 | + if using_copy_on_write: |
| 192 | + assert df._mgr._has_no_reference(0) |
| 193 | + assert df2._mgr._has_no_reference(0) |
| 194 | + assert not np.shares_memory(get_array(df, "a").codes, get_array(df2, "a").codes) |
| 195 | + tm.assert_frame_equal(df, df_orig) |
| 196 | + |
| 197 | + arr_a = get_array(df2, "a").codes |
| 198 | + df2.iloc[0, 0] = 2.0 |
| 199 | + assert np.shares_memory(get_array(df2, "a").codes, arr_a) |
40 | 200 |
|
41 | 201 |
|
42 | 202 | @pytest.mark.parametrize("method", ["where", "mask"])
|
|
0 commit comments