diff --git a/pandas/tests/copy_view/test_replace.py b/pandas/tests/copy_view/test_replace.py index a1347d8e12950..de7278dca06ff 100644 --- a/pandas/tests/copy_view/test_replace.py +++ b/pandas/tests/copy_view/test_replace.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas import ( Categorical, @@ -12,7 +13,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write): df = DataFrame({"a": Categorical([1, 2, 3])}) df_orig = df.copy() arr_a = get_array(df, "a") - view = df[:] # noqa + view = df[:] df.replace(to_replace=[1], value=2, inplace=True) if using_copy_on_write: @@ -27,7 +28,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write): def test_replace_inplace_reference(using_copy_on_write): df = DataFrame({"a": [1.5, 2, 3]}) arr_a = get_array(df, "a") - view = df[:] # noqa + view = df[:] df.replace(to_replace=[1.5], value=15.5, inplace=True) if using_copy_on_write: @@ -36,3 +37,22 @@ def test_replace_inplace_reference(using_copy_on_write): assert view._mgr._has_no_reference(0) else: assert np.shares_memory(get_array(df, "a"), arr_a) + + +@pytest.mark.parametrize("method", ["where", "mask"]) +def test_masking_inplace(using_copy_on_write, method): + df = DataFrame({"a": [1.5, 2, 3]}) + df_orig = df.copy() + arr_a = get_array(df, "a") + view = df[:] + + method = getattr(df, method) + method(df["a"] > 1.6, -1, inplace=True) + + if using_copy_on_write: + assert not np.shares_memory(get_array(df, "a"), arr_a) + assert df._mgr._has_no_reference(0) + assert view._mgr._has_no_reference(0) + tm.assert_frame_equal(view, df_orig) + else: + assert np.shares_memory(get_array(df, "a"), arr_a)