Skip to content

Fix: Using at indexer reassign existing field error #49113

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

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 8 additions & 5 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,22 +2426,25 @@ def _axes_are_unique(self) -> bool:

def __getitem__(self, key):

if self.ndim == 2 and not self._axes_are_unique:
if self.ndim == 2:
# GH#33041 fall back to .loc
if not isinstance(key, tuple) or not all(is_scalar(x) for x in key):
raise ValueError("Invalid call for scalar access (getting)!")
return self.obj.loc[key]

if not self._axes_are_unique:
return self.obj.loc[key]

return super().__getitem__(key)

def __setitem__(self, key, value):
if self.ndim == 2 and not self._axes_are_unique:
if self.ndim == 2:
# GH#33041 fall back to .loc
if not isinstance(key, tuple) or not all(is_scalar(x) for x in key):
raise ValueError("Invalid call for scalar access (setting)!")

self.obj.loc[key] = value
return
if not self._axes_are_unique:
self.obj.loc[key] = value
return

return super().__setitem__(key, value)

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexing/test_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,11 @@ def test_at_categorical_integers(self):
for key in [0, 1]:
with pytest.raises(KeyError, match=str(key)):
df.at[key, key]

def test_at_with_two_values(self):
# GH#48224
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

msg = "Invalid call for scalar access"
with pytest.raises(ValueError, match=msg):
df.at[slice(0, 1, None), "c"] = 7