From 3c3781c20a59193b5c2de643bff73151008e0707 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 20 Jan 2023 22:12:39 +0000 Subject: [PATCH 1/2] ENH: Add tst for asfreq CoW when doing noop --- pandas/tests/copy_view/test_methods.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 8fd9d5c5126c1..fd46fa5dd100a 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1044,6 +1044,29 @@ def test_putmask(using_copy_on_write): assert view.iloc[0, 0] == 5 +def test_asfreq_noop(using_copy_on_write): + df = DataFrame( + { + "a": Series( + [0.0, None, 2.0, 3.0], index=date_range("1/1/2000", periods=4, freq="T") + ) + } + ) + df_orig = df.copy() + df2 = df.asfreq(freq="T") + + 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 + + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) + tm.assert_frame_equal(df, df_orig) + + def test_isetitem(using_copy_on_write): df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}) df_orig = df.copy() From d4429e0c8e63723e3ffa8641322c7c1384e0f526 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Mon, 23 Jan 2023 20:33:30 -0500 Subject: [PATCH 2/2] Update pandas/tests/copy_view/test_methods.py Co-authored-by: Joris Van den Bossche --- pandas/tests/copy_view/test_methods.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 3c8f85d4a0877..a98e2df7a7db1 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1119,11 +1119,8 @@ def test_putmask(using_copy_on_write): def test_asfreq_noop(using_copy_on_write): df = DataFrame( - { - "a": Series( - [0.0, None, 2.0, 3.0], index=date_range("1/1/2000", periods=4, freq="T") - ) - } + {"a": [0.0, None, 2.0, 3.0]}, + index=date_range("1/1/2000", periods=4, freq="T"), ) df_orig = df.copy() df2 = df.asfreq(freq="T")