-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Datetimelike __setitem__ #24477
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
Datetimelike __setitem__ #24477
Changes from all commits
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 |
---|---|---|
|
@@ -478,6 +478,56 @@ def __getitem__(self, key): | |
|
||
return self._simple_new(result, **attribs) | ||
|
||
def __setitem__( | ||
self, | ||
key, # type: Union[int, Sequence[int], Sequence[bool], slice] | ||
value, # type: Union[NaTType, Scalar, Sequence[Scalar]] | ||
): | ||
# type: (...) -> None | ||
# I'm fudging the types a bit here. The "Scalar" above really depends | ||
# on type(self). For PeriodArray, it's Period (or stuff coercible | ||
# to a period in from_sequence). For DatetimeArray, it's Timestamp... | ||
# I don't know if mypy can do that, possibly with Generics. | ||
# https://mypy.readthedocs.io/en/latest/generics.html | ||
|
||
if is_list_like(value): | ||
is_slice = isinstance(key, slice) | ||
|
||
if lib.is_scalar(key): | ||
raise ValueError("setting an array element with a sequence.") | ||
|
||
if (not is_slice | ||
and len(key) != len(value) | ||
and not com.is_bool_indexer(key)): | ||
msg = ("shape mismatch: value array of length '{}' does not " | ||
"match indexing result of length '{}'.") | ||
raise ValueError(msg.format(len(key), len(value))) | ||
if not is_slice and len(key) == 0: | ||
return | ||
|
||
value = type(self)._from_sequence(value, dtype=self.dtype) | ||
self._check_compatible_with(value) | ||
value = value.asi8 | ||
elif isinstance(value, self._scalar_type): | ||
self._check_compatible_with(value) | ||
value = self._unbox_scalar(value) | ||
elif isna(value) or value == iNaT: | ||
value = iNaT | ||
else: | ||
msg = ( | ||
"'value' should be a '{scalar}', 'NaT', or array of those. " | ||
"Got '{typ}' instead." | ||
) | ||
raise TypeError(msg.format(scalar=self._scalar_type.__name__, | ||
typ=type(value).__name__)) | ||
self._data[key] = value | ||
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. would be nice to move smoe of this logic a big higher up to EA base if possible and/or make some helper methods to avoid code duplication of EA's implementating setitem, but for another time. |
||
self._maybe_clear_freq() | ||
|
||
def _maybe_clear_freq(self): | ||
# inplace operations like __setitem__ may invalidate the freq of | ||
# DatetimeArray and TimedeltaArray | ||
pass | ||
|
||
def astype(self, dtype, copy=True): | ||
# Some notes on cases we don't have to handle here in the base class: | ||
# 1. PeriodArray.astype handles period -> period | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,8 @@ def astype(self, dtype, copy=True): | |
|
||
def __setitem__(self, key, value): | ||
if pd.api.types.is_list_like(value): | ||
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. should prob import these like we normally do (is_like_like) at the top, but no big deal |
||
if pd.api.types.is_scalar(key): | ||
raise ValueError("setting an array element with a sequence.") | ||
value = [decimal.Decimal(v) for v in value] | ||
else: | ||
value = decimal.Decimal(value) | ||
|
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.
key could be a scalar here right? (in which case u will get an odd exception about len of unsized object)
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.
Hrm... yeah
This is a gap in the base extension tests as well. I'll ad one. that should be a... what? ValueError for trying to set list-like into a single element?
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.
NumPy raises with
ValueError: setting an array element with a sequence.
which seems like a fine error message to me.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.
yep seems good
maybe just let it fall thru this if will work?
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.
Seemed a tad easier to explicitly check for a scalar
key
here.Actually, grr, this is kinda annoying but NumPy allows setting a sequence of length 1.
but now I would raise on that.
Thoughts? I think this should raise, since a length-1 sequence is more like a sequence than a scalar.
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.
yeah i think we raise on this
this is why setitem in Block is so complicated :)