From b09a160b0fc93a97ed27d2aeb62cf1a19e6b7565 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:41:08 +0100 Subject: [PATCH 1/7] CoW: Warn for cases that go through putmask --- pandas/tests/apply/test_invalid_arg.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 9f5157181843e..80b2cce8c1dbc 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -18,7 +18,6 @@ DataFrame, Series, date_range, - notna, ) import pandas._testing as tm @@ -150,8 +149,6 @@ def test_transform_axis_1_raises(): Series([1]).transform("sum", axis=1) -# TODO(CoW-warn) should not need to warn -@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") def test_apply_modify_traceback(): data = DataFrame( { @@ -207,11 +204,6 @@ def transform(row): row["D"] = 7 return row - def transform2(row): - if notna(row["C"]) and row["C"].startswith("shin") and row["A"] == "foo": - row["D"] = 7 - return row - msg = "'float' object has no attribute 'startswith'" with pytest.raises(AttributeError, match=msg): data.apply(transform, axis=1) From 14fa1eda45cb2b8a831e77d7863ed30097c2926a Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:43:25 +0100 Subject: [PATCH 2/7] Remove another todo --- pandas/core/config_init.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index a8b63f97141c2..ff7064b760d88 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -476,9 +476,7 @@ def use_inf_as_na_cb(key) -> None: "copy_on_write", # Get the default from an environment variable, if set, otherwise defaults # to False. This environment variable can be set for testing. - "warn" - if os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "warn" - else os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "1", + "warn", copy_on_write_doc, validator=is_one_of_factory([True, False, "warn"]), ) From 1f3d62e346121ce385fff40526b87c2c4cf6d93c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:43:54 +0100 Subject: [PATCH 3/7] Fix --- pandas/core/config_init.py | 4 +++- pandas/tests/copy_view/test_constructors.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index ff7064b760d88..a8b63f97141c2 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -476,7 +476,9 @@ def use_inf_as_na_cb(key) -> None: "copy_on_write", # Get the default from an environment variable, if set, otherwise defaults # to False. This environment variable can be set for testing. - "warn", + "warn" + if os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "warn" + else os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "1", copy_on_write_doc, validator=is_one_of_factory([True, False, "warn"]), ) diff --git a/pandas/tests/copy_view/test_constructors.py b/pandas/tests/copy_view/test_constructors.py index 89384a4ef6ba6..7d5c485958039 100644 --- a/pandas/tests/copy_view/test_constructors.py +++ b/pandas/tests/copy_view/test_constructors.py @@ -297,7 +297,6 @@ def test_dataframe_from_series_or_index( if using_copy_on_write: assert not df._mgr._has_no_reference(0) - # TODO(CoW-warn) should not warn for an index? with tm.assert_cow_warning(warn_copy_on_write): df.iloc[0, 0] = data[-1] if using_copy_on_write: From 11d6216dc12c6e51ac66c01c7ab148966e15325d Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:25:40 +0100 Subject: [PATCH 4/7] Fixup --- pandas/core/config_init.py | 4 +--- pandas/tests/apply/test_invalid_arg.py | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index a8b63f97141c2..ff7064b760d88 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -476,9 +476,7 @@ def use_inf_as_na_cb(key) -> None: "copy_on_write", # Get the default from an environment variable, if set, otherwise defaults # to False. This environment variable can be set for testing. - "warn" - if os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "warn" - else os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "1", + "warn", copy_on_write_doc, validator=is_one_of_factory([True, False, "warn"]), ) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 80b2cce8c1dbc..b10b8503c9220 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -149,7 +149,7 @@ def test_transform_axis_1_raises(): Series([1]).transform("sum", axis=1) -def test_apply_modify_traceback(): +def test_apply_modify_traceback(warn_copy_on_write): data = DataFrame( { "A": [ @@ -206,7 +206,8 @@ def transform(row): msg = "'float' object has no attribute 'startswith'" with pytest.raises(AttributeError, match=msg): - data.apply(transform, axis=1) + with tm.assert_cow_warning(warn_copy_on_write): + data.apply(transform, axis=1) @pytest.mark.parametrize( From 47b31d45df2956665e7e14f3f82b8519595d023a Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:30:12 +0100 Subject: [PATCH 5/7] Update config_init.py --- pandas/core/config_init.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index ff7064b760d88..a8b63f97141c2 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -476,7 +476,9 @@ def use_inf_as_na_cb(key) -> None: "copy_on_write", # Get the default from an environment variable, if set, otherwise defaults # to False. This environment variable can be set for testing. - "warn", + "warn" + if os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "warn" + else os.environ.get("PANDAS_COPY_ON_WRITE", "0") == "1", copy_on_write_doc, validator=is_one_of_factory([True, False, "warn"]), ) From 77de8193ffa52fe24564cf99a1bd7b7cc77d030d Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:57:47 +0100 Subject: [PATCH 6/7] Remove another todo --- pandas/tests/copy_view/test_indexing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/copy_view/test_indexing.py b/pandas/tests/copy_view/test_indexing.py index 2e623f885b648..72b7aea3709c0 100644 --- a/pandas/tests/copy_view/test_indexing.py +++ b/pandas/tests/copy_view/test_indexing.py @@ -913,7 +913,6 @@ def test_del_frame(backend, using_copy_on_write, warn_copy_on_write): tm.assert_frame_equal(df2, df_orig[["a", "c"]]) df2._mgr._verify_integrity() - # TODO(CoW-warn) false positive, this should not warn? with tm.assert_cow_warning(warn_copy_on_write and dtype_backend == "numpy"): df.loc[0, "b"] = 200 assert np.shares_memory(get_array(df, "a"), get_array(df2, "a")) From 61fffb5c2f3c2726c264e5a3d0afaff73e3b5dd7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:39:33 +0100 Subject: [PATCH 7/7] Revert "CoW: Warn for cases that go through putmask" This reverts commit b09a160b --- pandas/tests/apply/test_invalid_arg.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index b10b8503c9220..53de64c72674b 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -18,6 +18,7 @@ DataFrame, Series, date_range, + notna, ) import pandas._testing as tm @@ -149,6 +150,8 @@ def test_transform_axis_1_raises(): Series([1]).transform("sum", axis=1) +# TODO(CoW-warn) should not need to warn +@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") def test_apply_modify_traceback(warn_copy_on_write): data = DataFrame( { @@ -204,6 +207,11 @@ def transform(row): row["D"] = 7 return row + def transform2(row): + if notna(row["C"]) and row["C"].startswith("shin") and row["A"] == "foo": + row["D"] = 7 + return row + msg = "'float' object has no attribute 'startswith'" with pytest.raises(AttributeError, match=msg): with tm.assert_cow_warning(warn_copy_on_write):