Skip to content

Commit cf78582

Browse files
CoW: Add warning for mask, where and clip with inplace (#56067)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 17eec96 commit cf78582

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

pandas/core/generic.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -8903,6 +8903,18 @@ def clip(
89038903
ChainedAssignmentError,
89048904
stacklevel=2,
89058905
)
8906+
elif not PYPY and not using_copy_on_write():
8907+
ctr = sys.getrefcount(self)
8908+
ref_count = REF_COUNT
8909+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
8910+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
8911+
ref_count += 1
8912+
if ctr <= ref_count:
8913+
warnings.warn(
8914+
_chained_assignment_warning_method_msg,
8915+
FutureWarning,
8916+
stacklevel=2,
8917+
)
89068918

89078919
axis = nv.validate_clip_with_axis(axis, (), kwargs)
89088920
if axis is not None:
@@ -10803,6 +10815,19 @@ def where(
1080310815
ChainedAssignmentError,
1080410816
stacklevel=2,
1080510817
)
10818+
elif not PYPY and not using_copy_on_write():
10819+
ctr = sys.getrefcount(self)
10820+
ref_count = REF_COUNT
10821+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
10822+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
10823+
ref_count += 1
10824+
if ctr <= ref_count:
10825+
warnings.warn(
10826+
_chained_assignment_warning_method_msg,
10827+
FutureWarning,
10828+
stacklevel=2,
10829+
)
10830+
1080610831
other = common.apply_if_callable(other, self)
1080710832
return self._where(cond, other, inplace, axis, level)
1080810833

@@ -10869,14 +10894,27 @@ def mask(
1086910894
ChainedAssignmentError,
1087010895
stacklevel=2,
1087110896
)
10897+
elif not PYPY and not using_copy_on_write():
10898+
ctr = sys.getrefcount(self)
10899+
ref_count = REF_COUNT
10900+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
10901+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
10902+
ref_count += 1
10903+
if ctr <= ref_count:
10904+
warnings.warn(
10905+
_chained_assignment_warning_method_msg,
10906+
FutureWarning,
10907+
stacklevel=2,
10908+
)
1087210909

1087310910
cond = common.apply_if_callable(cond, self)
10911+
other = common.apply_if_callable(other, self)
1087410912

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

10879-
return self.where(
10917+
return self._where(
1088010918
~cond,
1088110919
other=other,
1088210920
inplace=inplace,

pandas/tests/copy_view/test_clip.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import numpy as np
22

3-
from pandas import DataFrame
3+
from pandas import (
4+
DataFrame,
5+
option_context,
6+
)
47
import pandas._testing as tm
58
from pandas.tests.copy_view.util import get_array
69

@@ -81,3 +84,14 @@ def test_clip_chained_inplace(using_copy_on_write):
8184
with tm.raises_chained_assignment_error():
8285
df[["a"]].clip(1, 2, inplace=True)
8386
tm.assert_frame_equal(df, df_orig)
87+
else:
88+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
89+
df["a"].clip(1, 2, inplace=True)
90+
91+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
92+
with option_context("mode.chained_assignment", None):
93+
df[["a"]].clip(1, 2, inplace=True)
94+
95+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
96+
with option_context("mode.chained_assignment", None):
97+
df[df["a"] > 1].clip(1, 2, inplace=True)

pandas/tests/copy_view/test_methods.py

+11
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,17 @@ def test_chained_where_mask(using_copy_on_write, func):
15511551
with tm.raises_chained_assignment_error():
15521552
getattr(df[["a"]], func)(df["a"] > 2, 5, inplace=True)
15531553
tm.assert_frame_equal(df, df_orig)
1554+
else:
1555+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
1556+
getattr(df["a"], func)(df["a"] > 2, 5, inplace=True)
1557+
1558+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
1559+
with option_context("mode.chained_assignment", None):
1560+
getattr(df[["a"]], func)(df["a"] > 2, 5, inplace=True)
1561+
1562+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
1563+
with option_context("mode.chained_assignment", None):
1564+
getattr(df[df["a"] > 1], func)(df["a"] > 2, 5, inplace=True)
15541565

15551566

15561567
def test_asfreq_noop(using_copy_on_write):

0 commit comments

Comments
 (0)