Skip to content

BUG(CoW): also raise for chained assignment for .at / .iat #62074

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

def __setitem__(self, key, value) -> None:
if not PYPY:
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, 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 @@ -2605,6 +2611,15 @@ 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:
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)

return super().__setitem__(key, value)


def _tuplify(ndim: int, loc: Hashable) -> tuple[Hashable | slice, ...]:
"""
Expand Down
68 changes: 68 additions & 0 deletions pandas/tests/copy_view/test_chained_assignment_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,71 @@ def test_frame_setitem(indexer):

with tm.raises_chained_assignment_error():
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 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):
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

with tm.raises_chained_assignment_error():
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 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):
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})

with tm.raises_chained_assignment_error():
df[0:3].loc[indexer] = 10


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

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 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 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 tm.raises_chained_assignment_error():
df[0:3].iat[0, 0] = 10
Loading