Skip to content

CoW: Add ChainedAssignmentError for update #54024

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 2 commits 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Copy-on-Write improvements
operating inplace like this will never work, since the selection behaves
as a temporary copy. This holds true for:

- DataFrame.update / Series.update
- DataFrame.fillna / Series.fillna

.. _whatsnew_210.enhancements.enhancement2:
Expand Down
2 changes: 1 addition & 1 deletion pandas/compat/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
PY311 = sys.version_info >= (3, 11)
PYPY = platform.python_implementation() == "PyPy"
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")

REF_COUNT = 2 if PY311 else 3

__all__ = [
"IS64",
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from pandas._libs.hashtable import duplicated
from pandas._libs.lib import is_range_indexer
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import (
function as nv,
Expand All @@ -57,6 +58,7 @@
from pandas.errors import (
ChainedAssignmentError,
InvalidIndexError,
_chained_assignment_method_msg,
_chained_assignment_msg,
)
from pandas.util._decorators import (
Expand Down Expand Up @@ -8501,6 +8503,14 @@ def update(
1 2 500
2 3 6
"""
if not PYPY and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
stacklevel=2,
)

from pandas.core.computation import expressions

# TODO: Support other joins
Expand Down
9 changes: 3 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@
WriteExcelBuffer,
npt,
)
from pandas.compat import (
PY311,
PYPY,
)
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -7092,8 +7090,7 @@ def fillna(
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
refcount = 2 if PY311 else 3
if sys.getrefcount(self) <= refcount:
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
)
from pandas._libs.lib import is_range_indexer
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat.numpy import function as nv
from pandas.errors import (
ChainedAssignmentError,
InvalidIndexError,
_chained_assignment_method_msg,
_chained_assignment_msg,
)
from pandas.util._decorators import (
Expand Down Expand Up @@ -3435,6 +3437,13 @@ def update(self, other: Series | Sequence | Mapping) -> None:
2 3
dtype: int64
"""
if not PYPY and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
stacklevel=2,
)

if not isinstance(other, Series):
other = Series(other)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,20 @@ def test_update_series(using_copy_on_write):
tm.assert_series_equal(view, expected)


def test_update_chained_assignment(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3]})
ser2 = Series([100.0], index=[1])
df_orig = df.copy()
if using_copy_on_write:
with tm.raises_chained_assignment_error():
df["a"].update(ser2)
tm.assert_frame_equal(df, df_orig)

with tm.raises_chained_assignment_error():
df[["a"]].update(ser2.to_frame())
tm.assert_frame_equal(df, df_orig)


def test_inplace_arithmetic_series():
ser = Series([1, 2, 3])
data = get_array(ser)
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ def test_underlying_data_conversion(using_copy_on_write):
df["val"] = 0
df_original = df.copy()
df
df["val"].update(s)

if using_copy_on_write:
with tm.raises_chained_assignment_error():
df["val"].update(s)
expected = df_original
else:
df["val"].update(s)
expected = DataFrame(
{"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3], "val": [0, 1, 0]}
)
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/methods/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def test_update(self, using_copy_on_write):
df["c"] = df["c"].astype(object)
df_orig = df.copy()

df["c"].update(Series(["foo"], index=[0]))
if using_copy_on_write:
with tm.raises_chained_assignment_error():
df["c"].update(Series(["foo"], index=[0]))
expected = df_orig
else:
df["c"].update(Series(["foo"], index=[0]))
expected = DataFrame(
[[1, np.nan, "foo"], [3, 2.0, np.nan]], columns=["a", "b", "c"]
)
Expand Down