-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.at with non-unique axes #33047
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
Changes from all commits
8667c3a
3383a0d
42dfe29
6b044ca
02f6548
65aaa05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2045,6 +2045,7 @@ def __setitem__(self, key, value): | |
key = _tuplify(self.ndim, key) | ||
if len(key) != self.ndim: | ||
raise ValueError("Not enough indexers for scalar access (setting)!") | ||
|
||
key = list(self._convert_key(key, is_setter=True)) | ||
self.obj._set_value(*key, value=value, takeable=self._takeable) | ||
|
||
|
@@ -2064,15 +2065,32 @@ def _convert_key(self, key, is_setter: bool = False): | |
|
||
return key | ||
|
||
@property | ||
def _axes_are_unique(self) -> bool: | ||
# Only relevant for self.ndim == 2 | ||
assert self.ndim == 2 | ||
return self.obj.index.is_unique and self.obj.columns.is_unique | ||
|
||
def __getitem__(self, key): | ||
if self.ndim != 1 or not is_scalar(key): | ||
# FIXME: is_scalar check is a kludge | ||
return super().__getitem__(key) | ||
|
||
# Like Index.get_value, but we do not allow positional fallback | ||
obj = self.obj | ||
loc = obj.index.get_loc(key) | ||
return obj.index._get_values_for_loc(obj, loc, key) | ||
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): | ||
raise ValueError("Invalid call for scalar access (getting)!") | ||
return self.obj.loc[key] | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One problem with this, though, is that it allows any kind of indexer for this case (not only scalar ones), and thus relaxing the requirements for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to address this issue |
||
|
||
return super().__getitem__(key) | ||
|
||
def __setitem__(self, key, value): | ||
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): | ||
raise ValueError("Invalid call for scalar access (setting)!") | ||
|
||
self.obj.loc[key] = value | ||
return | ||
|
||
return super().__setitem__(key, value) | ||
|
||
|
||
@doc(IndexingMixin.iat) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add this assertion explicity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated+green