Skip to content

Commit

Permalink
CoW: Add warning for interpolate with inplace (#56066)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Nov 22, 2023
1 parent 34f39a9 commit f331b83
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
37 changes: 37 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7462,6 +7462,18 @@ def ffill(
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",
Expand Down Expand Up @@ -7633,6 +7645,19 @@ def bfill(
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,
Expand Down Expand Up @@ -8227,6 +8252,18 @@ def interpolate(
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)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/copy_view/test_interp_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ def test_interp_inplace(self, using_copy_on_write):
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)

Expand Down

0 comments on commit f331b83

Please sign in to comment.