Skip to content

CoW: Add warning for interpolate with inplace #56066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7450,6 +7450,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 @@ -7621,6 +7633,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 @@ -8215,6 +8240,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
12 changes: 12 additions & 0 deletions pandas/tests/copy_view/test_interp_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Series,
Timestamp,
interval_range,
option_context,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
Expand Down Expand Up @@ -378,3 +379,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