diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 54cf8e12cfcf8..30c7abfee26a3 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -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) diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index f6d2fb12c5d81..8ae4b4ea0b894 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -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