Skip to content

[backport 2.3.x] BUG(CoW): also raise for chained assignment for .at / .iat (#62074) #62115

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
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
35 changes: 35 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,22 @@ def __getitem__(self, key):
return super().__getitem__(key)

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
ctr = sys.getrefcount(self.obj)
ref_count = 2
if not warn_copy_on_write() and _check_cacher(self.obj):
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
ref_count += 1
if ctr <= ref_count:
warnings.warn(
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
)

if self.ndim == 2 and not self._axes_are_unique:
# GH#33041 fall back to .loc
if not isinstance(key, tuple) or not all(is_scalar(x) for x in key):
Expand All @@ -2599,6 +2615,25 @@ def _convert_key(self, key):
raise ValueError("iAt based indexing can only have integer indexers")
return key

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
ctr = sys.getrefcount(self.obj)
ref_count = 2
if not warn_copy_on_write() and _check_cacher(self.obj):
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
ref_count += 1
if ctr <= ref_count:
warnings.warn(
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
)

return super().__setitem__(key, value)


def _tuplify(ndim: int, loc: Hashable) -> tuple[Hashable | slice, ...]:
"""
Expand Down
80 changes: 80 additions & 0 deletions pandas/tests/copy_view/test_chained_assignment_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,83 @@ def test_frame_setitem(indexer, using_copy_on_write):
with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error(extra_warnings=extra_warnings):
df[0:3][indexer] = 10


@pytest.mark.parametrize(
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
)
def test_series_iloc_setitem(indexer):
df = DataFrame({"a": [1, 2, 3], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df["a"].iloc[indexer] = 0


@pytest.mark.parametrize(
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
)
def test_frame_iloc_setitem(indexer, using_copy_on_write):
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

extra_warnings = () if using_copy_on_write else (SettingWithCopyWarning,)

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error(extra_warnings=extra_warnings):
df[0:3].iloc[indexer] = 10


@pytest.mark.parametrize(
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
)
def test_series_loc_setitem(indexer):
df = DataFrame({"a": [1, 2, 3], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df["a"].loc[indexer] = 0


@pytest.mark.parametrize(
"indexer", [0, [0, 1], (0, "a"), slice(0, 2), np.array([True, False, True])]
)
def test_frame_loc_setitem(indexer, using_copy_on_write):
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

extra_warnings = () if using_copy_on_write else (SettingWithCopyWarning,)

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error(extra_warnings=extra_warnings):
df[0:3].loc[indexer] = 10


def test_series_at_setitem():
df = DataFrame({"a": [1, 2, 3], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df["a"].at[0] = 0


def test_frame_at_setitem():
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df[0:3].at[0, "a"] = 10


def test_series_iat_setitem():
df = DataFrame({"a": [1, 2, 3], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df["a"].iat[0] = 0


def test_frame_iat_setitem():
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

with option_context("chained_assignment", "warn"):
with tm.raises_chained_assignment_error():
df[0:3].iat[0, 0] = 10
Loading