From ab842620957baf00306e5b23ab497db3f600c264 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Fri, 10 Feb 2023 11:55:30 -0500 Subject: [PATCH 1/2] TST: Test mask/where inplace don't modify views with CoW --- pandas/tests/copy_view/test_replace.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pandas/tests/copy_view/test_replace.py b/pandas/tests/copy_view/test_replace.py index a1347d8e12950..e9438a5a154a5 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, @@ -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[:] # noqa + + 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) From 1e3615fd3fff0f6cdf8d4fc9cb1e2a3eeb1e0538 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Fri, 10 Feb 2023 12:14:18 -0500 Subject: [PATCH 2/2] remove noqas --- pandas/tests/copy_view/test_replace.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/copy_view/test_replace.py b/pandas/tests/copy_view/test_replace.py index e9438a5a154a5..de7278dca06ff 100644 --- a/pandas/tests/copy_view/test_replace.py +++ b/pandas/tests/copy_view/test_replace.py @@ -13,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: @@ -28,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: @@ -44,7 +44,7 @@ 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[:] # noqa + view = df[:] method = getattr(df, method) method(df["a"] > 1.6, -1, inplace=True)