Skip to content

Commit 5625236

Browse files
authored
CoW: Add warning for replace with inplace (#56060)
1 parent b2b0f97 commit 5625236

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

pandas/core/generic.py

+17
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
SettingWithCopyError,
101101
SettingWithCopyWarning,
102102
_chained_assignment_method_msg,
103+
_chained_assignment_warning_method_msg,
103104
)
104105
from pandas.util._decorators import (
105106
deprecate_nonkeyword_arguments,
@@ -7773,6 +7774,22 @@ def replace(
77737774
ChainedAssignmentError,
77747775
stacklevel=2,
77757776
)
7777+
elif not PYPY and not using_copy_on_write():
7778+
ctr = sys.getrefcount(self)
7779+
ref_count = REF_COUNT
7780+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7781+
# in non-CoW mode, chained Series access will populate the
7782+
# `_item_cache` which results in an increased ref count not below
7783+
# the threshold, while we still need to warn. We detect this case
7784+
# of a Series derived from a DataFrame through the presence of
7785+
# `_cacher`
7786+
ref_count += 1
7787+
if ctr <= ref_count:
7788+
warnings.warn(
7789+
_chained_assignment_warning_method_msg,
7790+
FutureWarning,
7791+
stacklevel=2,
7792+
)
77767793

77777794
if not is_bool(regex) and to_replace is not None:
77787795
raise ValueError("'to_replace' must be 'None' if 'regex' is not a bool")

pandas/errors/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,19 @@ class ChainedAssignmentError(Warning):
503503
)
504504

505505

506+
_chained_assignment_warning_method_msg = (
507+
"A value is trying to be set on a copy of a DataFrame or Series "
508+
"through chained assignment using an inplace method.\n"
509+
"The behavior will change in pandas 3.0. This inplace method will "
510+
"never work because the intermediate object on which we are setting "
511+
"values always behaves as a copy.\n\n"
512+
"For example, when doing 'df[col].method(value, inplace=True)', try "
513+
"using 'df.method({col: value}, inplace=True)' or "
514+
"df[col] = df[col].method(value) instead, to perform "
515+
"the operation inplace on the original object.\n\n"
516+
)
517+
518+
506519
class NumExprClobberingError(NameError):
507520
"""
508521
Exception raised when trying to use a built-in numexpr name as a variable name.

pandas/tests/copy_view/test_replace.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pandas import (
55
Categorical,
66
DataFrame,
7+
option_context,
78
)
89
import pandas._testing as tm
910
from pandas.tests.copy_view.util import get_array
@@ -395,6 +396,17 @@ def test_replace_chained_assignment(using_copy_on_write):
395396
with tm.raises_chained_assignment_error():
396397
df[["a"]].replace(1, 100, inplace=True)
397398
tm.assert_frame_equal(df, df_orig)
399+
else:
400+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
401+
with option_context("mode.chained_assignment", None):
402+
df[["a"]].replace(1, 100, inplace=True)
403+
404+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
405+
with option_context("mode.chained_assignment", None):
406+
df[df.a > 5].replace(1, 100, inplace=True)
407+
408+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
409+
df["a"].replace(1, 100, inplace=True)
398410

399411

400412
def test_replace_listlike(using_copy_on_write):

scripts/validate_unwanted_patterns.py

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"_global_config",
5151
"_chained_assignment_msg",
5252
"_chained_assignment_method_msg",
53+
"_chained_assignment_warning_method_msg",
5354
"_version_meson",
5455
# The numba extensions need this to mock the iloc object
5556
"_iLocIndexer",

0 commit comments

Comments
 (0)