Skip to content

Commit 9512e99

Browse files
CoW: better chained assignment warning message for update() method (#63500)
1 parent 1db90d5 commit 9512e99

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

pandas/_testing/contexts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
140140
else:
141141
warning = ChainedAssignmentError
142142
match = (
143-
"A value is trying to be set on a copy of a DataFrame or Series "
143+
"A value is being set on a copy of a DataFrame or Series "
144144
"through chained assignment"
145145
)
146146
if extra_warnings:

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
Pandas4Warning,
6363
)
6464
from pandas.errors.cow import (
65-
_chained_assignment_method_msg,
65+
_chained_assignment_method_update_msg,
6666
_chained_assignment_msg,
6767
)
6868
from pandas.util._decorators import (
@@ -10540,7 +10540,7 @@ def update(
1054010540
self
1054110541
) <= REF_COUNT_METHOD and not com.is_local_in_caller_frame(self):
1054210542
warnings.warn(
10543-
_chained_assignment_method_msg,
10543+
_chained_assignment_method_update_msg,
1054410544
ChainedAssignmentError,
1054510545
stacklevel=2,
1054610546
)

pandas/core/series.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
Pandas4Warning,
4848
)
4949
from pandas.errors.cow import (
50-
_chained_assignment_method_msg,
50+
_chained_assignment_method_update_msg,
5151
_chained_assignment_msg,
5252
)
5353
from pandas.util._decorators import (
@@ -3521,7 +3521,7 @@ def update(self, other: Series | Sequence | Mapping) -> None:
35213521
self
35223522
) <= REF_COUNT_METHOD and not com.is_local_in_caller_frame(self):
35233523
warnings.warn(
3524-
_chained_assignment_method_msg,
3524+
_chained_assignment_method_update_msg,
35253525
ChainedAssignmentError,
35263526
stacklevel=2,
35273527
)

pandas/errors/cow.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
_chained_assignment_msg = (
2-
"A value is trying to be set on a copy of a DataFrame or Series "
2+
"A value is being set on a copy of a DataFrame or Series "
33
"through chained assignment.\n"
44
"Such chained assignment never works to update the original DataFrame or "
55
"Series, because the intermediate object on which we are setting values "
@@ -13,7 +13,7 @@
1313

1414

1515
_chained_assignment_method_msg = (
16-
"A value is trying to be set on a copy of a DataFrame or Series "
16+
"A value is being set on a copy of a DataFrame or Series "
1717
"through chained assignment using an inplace method.\n"
1818
"Such inplace method never works to update the original DataFrame or Series, "
1919
"because the intermediate object on which we are setting values always "
@@ -26,3 +26,18 @@
2626
"https://pandas.pydata.org/pandas-docs/stable/user_guide/"
2727
"copy_on_write.html"
2828
)
29+
30+
31+
_chained_assignment_method_update_msg = (
32+
"A value is being set on a copy of a DataFrame or Series "
33+
"through chained assignment using an inplace method.\n"
34+
"Such inplace method never works to update the original DataFrame or Series, "
35+
"because the intermediate object on which we are setting values always "
36+
"behaves as a copy (due to Copy-on-Write).\n\n"
37+
"For example, when doing 'df[col].update(other)', try "
38+
"using 'df.update({col: other})' instead, to perform "
39+
"the operation inplace on the original object.\n\n"
40+
"See the documentation for a more detailed explanation: "
41+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/"
42+
"copy_on_write.html"
43+
)

pandas/tests/indexing/test_chaining_and_caching.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
)
1515
import pandas._testing as tm
1616

17-
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
18-
1917

2018
class TestCaching:
2119
@pytest.mark.parametrize("do_ref", [True, False])

scripts/validate_unwanted_patterns.py

Lines changed: 1 addition & 0 deletions
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_method_update_msg",
5354
"_version_meson",
5455
# The numba extensions need this to mock the iloc object
5556
"_iLocIndexer",

0 commit comments

Comments
 (0)