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
36 changes: 36 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8841,6 +8841,17 @@ 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
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 @@ -10741,6 +10752,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 @@ -10807,6 +10831,18 @@ 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)

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)
24 changes: 21 additions & 3 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Series,
Timestamp,
date_range,
option_context,
period_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -1550,6 +1551,23 @@ 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", check_stacklevel=False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the check_stacklevel=False needed here? (the warnings are added to top-level methods, and we are testing those directly, so that should be a simple case?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue, this showed up in a ci build only and only on one, didn't want to spend more time debugging this...

But can try to reproduce locally if you want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's just because mask is calling where under the hood. So it was actually raising the warning twice (once with correct and once with incorrect stacklevel).

Pushing a fix to let it only warn once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting

Not clue why this didn't fail locally though

):
getattr(df["a"], func)(df["a"] > 2, 5, inplace=True)

with tm.assert_produces_warning(
FutureWarning, match="inplace method", check_stacklevel=False
):
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", check_stacklevel=False
):
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 Expand Up @@ -1684,7 +1702,7 @@ def test_get(using_copy_on_write, warn_copy_on_write, key):
warn = FutureWarning if isinstance(key, str) else None
else:
warn = SettingWithCopyWarning if isinstance(key, list) else None
with pd.option_context("chained_assignment", "warn"):
with option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
result.iloc[0] = 0

Expand Down Expand Up @@ -1721,7 +1739,7 @@ def test_xs(
with tm.assert_cow_warning(single_block or axis == 1):
result.iloc[0] = 0
else:
with pd.option_context("chained_assignment", "warn"):
with option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(SettingWithCopyWarning):
result.iloc[0] = 0

Expand Down Expand Up @@ -1756,7 +1774,7 @@ def test_xs_multiindex(
warn = SettingWithCopyWarning
else:
warn = None
with pd.option_context("chained_assignment", "warn"):
with option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
result.iloc[0, 0] = 0

Expand Down
Loading