Repository: pandas-dev/pandas
Pull request #56066 (2023-11-19 16:30:31)

CoW: Add warning for interpolate with inplace
xref https://github.com/pandas-dev/pandas/issues/56019

jorisvandenbossche: @phofl something went wrong in the update here, includes lots of commits
phofl: Fixed, now clue how that happened

Patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index d723eb622b..0d95d19781 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -7462,6 +7462,18 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
                         ChainedAssignmentError,
                         stacklevel=2,
                     )
+            elif not PYPY and not using_copy_on_write():
+                ctr = sys.getrefcount(self)
+                ref_count = REF_COUNT
+                if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
+                    # see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
+                    ref_count += 1
+                if ctr <= ref_count:
+                    warnings.warn(
+                        _chained_assignment_warning_method_msg,
+                        FutureWarning,
+                        stacklevel=2,
+                    )
 
         return self._pad_or_backfill(
             "ffill",
@@ -7633,6 +7645,19 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
                         ChainedAssignmentError,
                         stacklevel=2,
                     )
+            elif not PYPY and not using_copy_on_write():
+                ctr = sys.getrefcount(self)
+                ref_count = REF_COUNT
+                if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
+                    # see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
+                    ref_count += 1
+                if ctr <= ref_count:
+                    warnings.warn(
+                        _chained_assignment_warning_method_msg,
+                        FutureWarning,
+                        stacklevel=2,
+                    )
+
         return self._pad_or_backfill(
             "bfill",
             axis=axis,
@@ -8227,6 +8252,18 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
                         ChainedAssignmentError,
                         stacklevel=2,
                     )
+            elif not PYPY and not using_copy_on_write():
+                ctr = sys.getrefcount(self)
+                ref_count = REF_COUNT
+                if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
+                    # see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
+                    ref_count += 1
+                if ctr <= ref_count:
+                    warnings.warn(
+                        _chained_assignment_warning_method_msg,
+                        FutureWarning,
+                        stacklevel=2,
+                    )
 
         axis = self._get_axis_number(axis)
 


Test patch:
diff --git a/pandas/tests/copy_view/test_interp_fillna.py b/pandas/tests/copy_view/test_interp_fillna.py
index 9b6d383f4e..cdb06de90a 100644
--- a/pandas/tests/copy_view/test_interp_fillna.py
+++ b/pandas/tests/copy_view/test_interp_fillna.py
@@ -390,3 +390,14 @@ def test_interpolate_chained_assignment(using_copy_on_write, func):
         with tm.raises_chained_assignment_error():
             getattr(df[["a"]], func)(inplace=True)
         tm.assert_frame_equal(df, df_orig)
+    else:
+        with tm.assert_produces_warning(FutureWarning, match="inplace method"):
+            getattr(df["a"], func)(inplace=True)
+
+        with tm.assert_produces_warning(FutureWarning, match="inplace method"):
+            with option_context("mode.chained_assignment", None):
+                getattr(df[["a"]], func)(inplace=True)
+
+        with tm.assert_produces_warning(FutureWarning, match="inplace method"):
+            with option_context("mode.chained_assignment", None):
+                getattr(df[df["a"] > 1], func)(inplace=True)
diff --git a/pandas/tests/frame/methods/test_interpolate.py b/pandas/tests/frame/methods/test_interpolate.py
index 1ddab99947..5f37ed6d9e 100644
--- a/pandas/tests/frame/methods/test_interpolate.py
+++ b/pandas/tests/frame/methods/test_interpolate.py
@@ -378,7 +378,8 @@ class TestDataFrameInterpolate:
             assert return_value is None
             tm.assert_frame_equal(result, expected_cow)
         else:
-            return_value = result["a"].interpolate(inplace=True)
+            with tm.assert_produces_warning(FutureWarning, match="inplace method"):
+                return_value = result["a"].interpolate(inplace=True)
             assert return_value is None
             tm.assert_frame_equal(result, expected)