diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4a0f31357079f..66dce720d0a42 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7213,6 +7213,8 @@ def replace( if inplace: return self._update_inplace(result) else: + # GH 49473 Use "lazy copy" with Copy-on-Write + result = self.copy(deep=None) return result.__finalize__(self, method="replace") def interpolate( diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 956e2cf98c9b6..4f8bf5b00972f 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -214,3 +214,21 @@ def test_chained_methods(request, method, idx, using_copy_on_write): df.iloc[0, 0] = 0 if not df2_is_view: tm.assert_frame_equal(df2.iloc[:, idx:], df_orig) + + +def test_replace(using_copy_on_write): + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}) + df_orig = df.copy() + df2 = df.replace(1, 5) + df2._mgr._verify_integrity() + + if using_copy_on_write: + assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + else: + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + + # mutating df2 triggers a copy-on-write for that column/block + df2.iloc[0, 0] = 0 + if using_copy_on_write: + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + tm.assert_frame_equal(df, df_orig)