Skip to content
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

CoW: Add warning for mask, where and clip with inplace #56067

Merged
merged 9 commits into from
Nov 27, 2023
40 changes: 39 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8902,6 +8902,18 @@ def clip(
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"):
phofl marked this conversation as resolved.
Show resolved Hide resolved
# 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 = nv.validate_clip_with_axis(axis, (), kwargs)
if axis is not None:
Expand Down Expand Up @@ -10802,6 +10814,19 @@ def where(
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,
)

other = common.apply_if_callable(other, self)
return self._where(cond, other, inplace, axis, level)

Expand Down Expand Up @@ -10868,14 +10893,27 @@ def mask(
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,
)

cond = common.apply_if_callable(cond, self)
other = common.apply_if_callable(other, self)

# see gh-21891
if not hasattr(cond, "__invert__"):
cond = np.array(cond)

return self.where(
return self._where(
~cond,
other=other,
inplace=inplace,
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/copy_view/test_clip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np

from pandas import DataFrame
from pandas import (
DataFrame,
option_context,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array

Expand Down Expand Up @@ -81,3 +84,14 @@ def test_clip_chained_inplace(using_copy_on_write):
with tm.raises_chained_assignment_error():
df[["a"]].clip(1, 2, inplace=True)
tm.assert_frame_equal(df, df_orig)
else:
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
df["a"].clip(1, 2, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
df[["a"]].clip(1, 2, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
df[df["a"] > 1].clip(1, 2, inplace=True)
11 changes: 11 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,17 @@ def test_chained_where_mask(using_copy_on_write, func):
with tm.raises_chained_assignment_error():
getattr(df[["a"]], func)(df["a"] > 2, 5, inplace=True)
tm.assert_frame_equal(df, df_orig)
else:
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
getattr(df["a"], func)(df["a"] > 2, 5, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
getattr(df[["a"]], func)(df["a"] > 2, 5, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
getattr(df[df["a"] > 1], func)(df["a"] > 2, 5, inplace=True)


def test_asfreq_noop(using_copy_on_write):
Expand Down
Loading