Skip to content

Commit 1d38dc3

Browse files
TST/DOC: clarify warning message for inplace methods with CoW (#54081)
1 parent 7ed7c7a commit 1d38dc3

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

pandas/errors/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,13 @@ class ChainedAssignmentError(Warning):
395395

396396
_chained_assignment_method_msg = (
397397
"A value is trying to be set on a copy of a DataFrame or Series "
398-
"through chained assignment.\n"
399-
"When using the Copy-on-Write mode, such chained assignment never works "
398+
"through chained assignment using an inplace method.\n"
399+
"When using the Copy-on-Write mode, such inplace method never works "
400400
"to update the original DataFrame or Series, because the intermediate "
401401
"object on which we are setting values always behaves as a copy.\n\n"
402-
"Try using 'df.method({col: value}, inplace=True)' instead, to perform "
403-
"the operation inplace.\n\n"
402+
"For example, when doing 'df[col].method(value, inplace=True)', try "
403+
"using 'df.method({col: value}, inplace=True)' instead, to perform "
404+
"the operation inplace on the original object.\n\n"
404405
)
405406

406407

pandas/tests/frame/methods/test_fillna.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.errors import ChainedAssignmentError
54
import pandas.util._test_decorators as td
65

76
from pandas import (
@@ -51,7 +50,7 @@ def test_fillna_on_column_view(self, using_copy_on_write):
5150
df = DataFrame(arr, copy=False)
5251

5352
if using_copy_on_write:
54-
with tm.assert_produces_warning(ChainedAssignmentError):
53+
with tm.raises_chained_assignment_error():
5554
df[0].fillna(-1, inplace=True)
5655
assert np.isnan(arr[:, 0]).all()
5756
else:

pandas/tests/frame/test_block_internals.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import numpy as np
88
import pytest
99

10-
from pandas.errors import (
11-
ChainedAssignmentError,
12-
PerformanceWarning,
13-
)
10+
from pandas.errors import PerformanceWarning
1411
import pandas.util._test_decorators as td
1512

1613
import pandas as pd
@@ -414,7 +411,7 @@ def test_update_inplace_sets_valid_block_values(using_copy_on_write):
414411

415412
# inplace update of a single column
416413
if using_copy_on_write:
417-
with tm.assert_produces_warning(ChainedAssignmentError):
414+
with tm.raises_chained_assignment_error():
418415
df["a"].fillna(1, inplace=True)
419416
else:
420417
df["a"].fillna(1, inplace=True)

pandas/tests/indexing/multiindex/test_chaining_and_caching.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.errors import (
5-
ChainedAssignmentError,
6-
SettingWithCopyError,
7-
)
4+
from pandas.errors import SettingWithCopyError
85
import pandas.util._test_decorators as td
96

107
from pandas import (
@@ -33,7 +30,7 @@ def test_detect_chained_assignment(using_copy_on_write):
3330
zed = DataFrame(events, index=["a", "b"], columns=multiind)
3431

3532
if using_copy_on_write:
36-
with tm.assert_produces_warning(ChainedAssignmentError):
33+
with tm.raises_chained_assignment_error():
3734
zed["eyes"]["right"].fillna(value=555, inplace=True)
3835
else:
3936
msg = "A value is trying to be set on a copy of a slice from a DataFrame"

0 commit comments

Comments
 (0)