Skip to content

Commit f331b83

Browse files
authored
CoW: Add warning for interpolate with inplace (#56066)
1 parent 34f39a9 commit f331b83

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

pandas/core/generic.py

+37
Original file line numberDiff line numberDiff line change
@@ -7462,6 +7462,18 @@ def ffill(
74627462
ChainedAssignmentError,
74637463
stacklevel=2,
74647464
)
7465+
elif not PYPY and not using_copy_on_write():
7466+
ctr = sys.getrefcount(self)
7467+
ref_count = REF_COUNT
7468+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7469+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7470+
ref_count += 1
7471+
if ctr <= ref_count:
7472+
warnings.warn(
7473+
_chained_assignment_warning_method_msg,
7474+
FutureWarning,
7475+
stacklevel=2,
7476+
)
74657477

74667478
return self._pad_or_backfill(
74677479
"ffill",
@@ -7633,6 +7645,19 @@ def bfill(
76337645
ChainedAssignmentError,
76347646
stacklevel=2,
76357647
)
7648+
elif not PYPY and not using_copy_on_write():
7649+
ctr = sys.getrefcount(self)
7650+
ref_count = REF_COUNT
7651+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7652+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7653+
ref_count += 1
7654+
if ctr <= ref_count:
7655+
warnings.warn(
7656+
_chained_assignment_warning_method_msg,
7657+
FutureWarning,
7658+
stacklevel=2,
7659+
)
7660+
76367661
return self._pad_or_backfill(
76377662
"bfill",
76387663
axis=axis,
@@ -8227,6 +8252,18 @@ def interpolate(
82278252
ChainedAssignmentError,
82288253
stacklevel=2,
82298254
)
8255+
elif not PYPY and not using_copy_on_write():
8256+
ctr = sys.getrefcount(self)
8257+
ref_count = REF_COUNT
8258+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
8259+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
8260+
ref_count += 1
8261+
if ctr <= ref_count:
8262+
warnings.warn(
8263+
_chained_assignment_warning_method_msg,
8264+
FutureWarning,
8265+
stacklevel=2,
8266+
)
82308267

82318268
axis = self._get_axis_number(axis)
82328269

pandas/tests/copy_view/test_interp_fillna.py

+11
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,14 @@ def test_interpolate_chained_assignment(using_copy_on_write, func):
390390
with tm.raises_chained_assignment_error():
391391
getattr(df[["a"]], func)(inplace=True)
392392
tm.assert_frame_equal(df, df_orig)
393+
else:
394+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
395+
getattr(df["a"], func)(inplace=True)
396+
397+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
398+
with option_context("mode.chained_assignment", None):
399+
getattr(df[["a"]], func)(inplace=True)
400+
401+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
402+
with option_context("mode.chained_assignment", None):
403+
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)