Skip to content

TST/DOC: clarify warning message for inplace methods with CoW #54081

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

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,13 @@ class ChainedAssignmentError(Warning):

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


Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pytest

from pandas.errors import ChainedAssignmentError
import pandas.util._test_decorators as td

from pandas import (
Expand Down Expand Up @@ -51,7 +50,7 @@ def test_fillna_on_column_view(self, using_copy_on_write):
df = DataFrame(arr, copy=False)

if using_copy_on_write:
with tm.assert_produces_warning(ChainedAssignmentError):
with tm.raises_chained_assignment_error():
df[0].fillna(-1, inplace=True)
assert np.isnan(arr[:, 0]).all()
else:
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import numpy as np
import pytest

from pandas.errors import (
ChainedAssignmentError,
PerformanceWarning,
)
from pandas.errors import PerformanceWarning
import pandas.util._test_decorators as td

import pandas as pd
Expand Down Expand Up @@ -414,7 +411,7 @@ def test_update_inplace_sets_valid_block_values(using_copy_on_write):

# inplace update of a single column
if using_copy_on_write:
with tm.assert_produces_warning(ChainedAssignmentError):
with tm.raises_chained_assignment_error():
df["a"].fillna(1, inplace=True)
else:
df["a"].fillna(1, inplace=True)
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/indexing/multiindex/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import numpy as np
import pytest

from pandas.errors import (
ChainedAssignmentError,
SettingWithCopyError,
)
from pandas.errors import SettingWithCopyError
import pandas.util._test_decorators as td

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

if using_copy_on_write:
with tm.assert_produces_warning(ChainedAssignmentError):
with tm.raises_chained_assignment_error():
zed["eyes"]["right"].fillna(value=555, inplace=True)
else:
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
Expand Down