From a0b722888a9ed505cd4d95dbda02ea42fc35605a Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 15 Feb 2023 06:32:37 -0500 Subject: [PATCH 1/2] TST: clip inplace doesn't modify views with CoW --- pandas/tests/copy_view/test_clip.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 pandas/tests/copy_view/test_clip.py diff --git a/pandas/tests/copy_view/test_clip.py b/pandas/tests/copy_view/test_clip.py new file mode 100644 index 0000000000000..f5c735644a952 --- /dev/null +++ b/pandas/tests/copy_view/test_clip.py @@ -0,0 +1,21 @@ +import numpy as np + +from pandas import DataFrame +import pandas._testing as tm +from pandas.tests.copy_view.util import get_array + + +def test_clip_inplace_reference(using_copy_on_write): + df = DataFrame({"a": [1.5, 2, 3]}) + df_copy = df.copy() + arr_a = get_array(df, "a") + view = df[:] + df.clip(lower=1.6, 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(df_copy, view) + else: + assert np.shares_memory(get_array(df, "a"), arr_a) From 643402d58a1007299758d31ae856e6209b64e5ea Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 15 Feb 2023 07:20:14 -0500 Subject: [PATCH 2/2] fix test for non CoW --- pandas/tests/copy_view/test_clip.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/copy_view/test_clip.py b/pandas/tests/copy_view/test_clip.py index f5c735644a952..6626f724ab7f3 100644 --- a/pandas/tests/copy_view/test_clip.py +++ b/pandas/tests/copy_view/test_clip.py @@ -10,12 +10,12 @@ def test_clip_inplace_reference(using_copy_on_write): df_copy = df.copy() arr_a = get_array(df, "a") view = df[:] - df.clip(lower=1.6, inplace=True) + df.clip(lower=2, inplace=True) + + # Clip not actually inplace right now but could be + assert not np.shares_memory(get_array(df, "a"), arr_a) 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(df_copy, view) - else: - assert np.shares_memory(get_array(df, "a"), arr_a)