From 642430901fd32eb409570a7478f4f4b90821b17d Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 29 Nov 2023 00:25:57 +0100 Subject: [PATCH 1/3] CoW: Don't warn when updating with inplace op --- pandas/core/generic.py | 1 + pandas/tests/frame/methods/test_quantile.py | 2 -- pandas/tests/groupby/test_groupby.py | 2 -- pandas/tests/indexing/test_iloc.py | 4 +--- 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 56001fabfdc9d..c022c0650c0f2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -12500,6 +12500,7 @@ def _inplace_method(self, other, op) -> Self: and result._indexed_same(self) and result.dtype == self.dtype and not using_copy_on_write() + and not warn_copy_on_write() ): # GH#36498 this inplace op can _actually_ be inplace. # Item "ArrayManager" of "Union[ArrayManager, SingleArrayManager, diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 787b77a5c725a..0f27eae1a3bfc 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -110,8 +110,6 @@ def test_non_numeric_exclusion(self, interp_method, request, using_array_manager request.applymarker(pytest.mark.xfail(reason="Axis name incorrectly set.")) tm.assert_series_equal(rs, xp) - # TODO(CoW-warn) should not need to warn - @pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") def test_axis(self, interp_method, request, using_array_manager): # axis interpolation, method = interp_method diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 5b17484de9c93..3c6419ab86494 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -41,8 +41,6 @@ def test_repr(): assert result == expected -# TODO(CoW-warn) this should NOT warn -> inplace operator triggers it -@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") def test_groupby_std_datetimelike(warn_copy_on_write): # GH#48481 tdi = pd.timedelta_range("1 Day", periods=10000) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index baf2cdae43fe4..1e58ca77de7e8 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -426,9 +426,7 @@ def test_iloc_getitem_slice_dups(self): tm.assert_frame_equal(df.iloc[10:, :2], df2) tm.assert_frame_equal(df.iloc[10:, 2:], df1) - # TODO(CoW-warn) this should NOT warn -> Series inplace operator - @pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") - def test_iloc_setitem(self): + def test_iloc_setitem(self, warn_copy_on_write): df = DataFrame( np.random.default_rng(2).standard_normal((4, 4)), index=np.arange(0, 8, 2), From 612eb70c37daa5cfa033d329aeef5255d1d20976 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:09:48 +0100 Subject: [PATCH 2/3] CoW: Remove false positive warnings for inplace operators --- pandas/core/generic.py | 3 ++- pandas/tests/indexing/test_chaining_and_caching.py | 6 +----- pandas/tests/indexing/test_iloc.py | 6 ++---- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c022c0650c0f2..3c29b753ab7f1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -12494,13 +12494,14 @@ def _inplace_method(self, other, op) -> Self: warn = False result = op(self, other) + print(warn) if ( self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype and not using_copy_on_write() - and not warn_copy_on_write() + and not (warn_copy_on_write() and not warn) ): # GH#36498 this inplace op can _actually_ be inplace. # Item "ArrayManager" of "Union[ArrayManager, SingleArrayManager, diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index bf0975a803dce..a0ece7cd72cd8 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -121,11 +121,7 @@ def test_setitem_cache_updating_slices( out = DataFrame({"A": [0, 0, 0]}, index=date_range("5/7/2014", "5/9/2014")) for ix, row in df.iterrows(): - # TODO(CoW-warn) should not warn - with tm.assert_produces_warning( - FutureWarning if warn_copy_on_write else None - ): - out.loc[six:eix, row["C"]] += row["D"] + out.loc[six:eix, row["C"]] += row["D"] tm.assert_frame_equal(out, expected) tm.assert_series_equal(out["A"], expected["A"]) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 1e58ca77de7e8..31263b44ed205 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -1145,7 +1145,7 @@ def test_iloc_getitem_with_duplicates2(self): expected = df.take([0], axis=1) tm.assert_frame_equal(result, expected) - def test_iloc_interval(self, warn_copy_on_write): + def test_iloc_interval(self): # GH#17130 df = DataFrame({Interval(1, 2): [1, 2]}) @@ -1158,9 +1158,7 @@ def test_iloc_interval(self, warn_copy_on_write): tm.assert_series_equal(result, expected) result = df.copy() - # TODO(CoW-warn) false positive - with tm.assert_cow_warning(warn_copy_on_write): - result.iloc[:, 0] += 1 + result.iloc[:, 0] += 1 expected = DataFrame({Interval(1, 2): [2, 3]}) tm.assert_frame_equal(result, expected) From aead3f481f979be942bca5aa6a577d3282f7298e Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:11:09 +0100 Subject: [PATCH 3/3] Remove print --- pandas/core/generic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3c29b753ab7f1..30646cea706e5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -12494,7 +12494,6 @@ def _inplace_method(self, other, op) -> Self: warn = False result = op(self, other) - print(warn) if ( self.ndim == 1