From 040131b7710bd35a7a5ec97fe50cb6bc744379e1 Mon Sep 17 00:00:00 2001 From: seljaks <33955366+seljaks@users.noreply.github.com> Date: Tue, 29 Nov 2022 23:15:35 +0100 Subject: [PATCH 1/3] TST: CoW tests for df.head and df.tail GH49473 --- pandas/tests/copy_view/test_methods.py | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 0c68f6a866eec..eb4d55ee0530e 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -250,3 +250,33 @@ def test_set_index(using_copy_on_write): df2.iloc[0, 1] = 0 assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c")) tm.assert_frame_equal(df, df_orig) + + +@pytest.mark.parametrize( + "method", + [ + lambda df: df.head(), + lambda df: df.head(2), + lambda df: df.tail(), + lambda df: df.tail(3), + ], +) +def test_head_tail(using_copy_on_write, method): + df = DataFrame({"a": [1, 1, 1], "b": [0.1, 0.2, 0.3]}) + df_orig = df.copy() + df2 = method(df) + df2._mgr._verify_integrity() + + if using_copy_on_write: + assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + assert np.shares_memory(get_array(df2, "b"), get_array(df, "b")) + + # modify df2 to trigger CoW for that block + df2.iloc[0, 0] = 0 + assert np.shares_memory(get_array(df2, "b"), get_array(df, "b")) + if using_copy_on_write: + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + else: + # without CoW enabled, head and tail return views. Mutating df2 also mutates df. + df2.iloc[0, 0] = 1 + tm.assert_frame_equal(df, df_orig) From 0c5384ddfb93cd8ffc66d75d22dc9638e164ee2c Mon Sep 17 00:00:00 2001 From: seljaks <33955366+seljaks@users.noreply.github.com> Date: Tue, 29 Nov 2022 23:24:29 +0100 Subject: [PATCH 2/3] changed arg order --- pandas/tests/copy_view/test_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index eb4d55ee0530e..ad0a5799f9595 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -261,7 +261,7 @@ def test_set_index(using_copy_on_write): lambda df: df.tail(3), ], ) -def test_head_tail(using_copy_on_write, method): +def test_head_tail(method, using_copy_on_write): df = DataFrame({"a": [1, 1, 1], "b": [0.1, 0.2, 0.3]}) df_orig = df.copy() df2 = method(df) From 8451a316152aec61de02b1994edff5f507a2913e Mon Sep 17 00:00:00 2001 From: seljaks <33955366+seljaks@users.noreply.github.com> Date: Wed, 30 Nov 2022 13:43:44 +0100 Subject: [PATCH 3/3] made df values consistent --- pandas/tests/copy_view/test_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index ad0a5799f9595..bf65f153b10dd 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -262,7 +262,7 @@ def test_set_index(using_copy_on_write): ], ) def test_head_tail(method, using_copy_on_write): - df = DataFrame({"a": [1, 1, 1], "b": [0.1, 0.2, 0.3]}) + df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) df_orig = df.copy() df2 = method(df) df2._mgr._verify_integrity()