Skip to content

Commit 27eb8c7

Browse files
committed
CoW: Add warning for interpolate with inplace
1 parent 89185cd commit 27eb8c7

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

pandas/core/generic.py

+35
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
SettingWithCopyError,
101101
SettingWithCopyWarning,
102102
_chained_assignment_method_msg,
103+
_chained_assignment_warning_method_msg,
103104
)
104105
from pandas.util._decorators import (
105106
deprecate_nonkeyword_arguments,
@@ -7449,6 +7450,17 @@ def ffill(
74497450
ChainedAssignmentError,
74507451
stacklevel=2,
74517452
)
7453+
elif not PYPY and not using_copy_on_write():
7454+
ctr = sys.getrefcount(self)
7455+
ref_count = REF_COUNT
7456+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7457+
ref_count += 1
7458+
if ctr <= ref_count:
7459+
warnings.warn(
7460+
_chained_assignment_warning_method_msg,
7461+
FutureWarning,
7462+
stacklevel=2,
7463+
)
74527464

74537465
return self._pad_or_backfill(
74547466
"ffill",
@@ -7620,6 +7632,18 @@ def bfill(
76207632
ChainedAssignmentError,
76217633
stacklevel=2,
76227634
)
7635+
elif not PYPY and not using_copy_on_write():
7636+
ctr = sys.getrefcount(self)
7637+
ref_count = REF_COUNT
7638+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7639+
ref_count += 1
7640+
if ctr <= ref_count:
7641+
warnings.warn(
7642+
_chained_assignment_warning_method_msg,
7643+
FutureWarning,
7644+
stacklevel=2,
7645+
)
7646+
76237647
return self._pad_or_backfill(
76247648
"bfill",
76257649
axis=axis,
@@ -8198,6 +8222,17 @@ def interpolate(
81988222
ChainedAssignmentError,
81998223
stacklevel=2,
82008224
)
8225+
elif not PYPY and not using_copy_on_write():
8226+
ctr = sys.getrefcount(self)
8227+
ref_count = REF_COUNT
8228+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
8229+
ref_count += 1
8230+
if ctr <= ref_count:
8231+
warnings.warn(
8232+
_chained_assignment_warning_method_msg,
8233+
FutureWarning,
8234+
stacklevel=2,
8235+
)
82018236

82028237
axis = self._get_axis_number(axis)
82038238

pandas/tests/copy_view/test_interp_fillna.py

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Series,
1111
Timestamp,
1212
interval_range,
13+
option_context,
1314
)
1415
import pandas._testing as tm
1516
from pandas.tests.copy_view.util import get_array
@@ -378,3 +379,14 @@ def test_interpolate_chained_assignment(using_copy_on_write, func):
378379
with tm.raises_chained_assignment_error():
379380
getattr(df[["a"]], func)(inplace=True)
380381
tm.assert_frame_equal(df, df_orig)
382+
else:
383+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
384+
getattr(df["a"], func)(inplace=True)
385+
386+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
387+
with option_context("mode.chained_assignment", None):
388+
getattr(df[["a"]], func)(inplace=True)
389+
390+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
391+
with option_context("mode.chained_assignment", None):
392+
getattr(df[df["a"] > 1], func)(inplace=True)

pandas/tests/frame/methods/test_interpolate.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ def test_interp_inplace(self, using_copy_on_write):
378378
assert return_value is None
379379
tm.assert_frame_equal(result, expected_cow)
380380
else:
381-
return_value = result["a"].interpolate(inplace=True)
381+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
382+
return_value = result["a"].interpolate(inplace=True)
382383
assert return_value is None
383384
tm.assert_frame_equal(result, expected)
384385

0 commit comments

Comments
 (0)