-
-
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
Merged
Merged
Datetimelike __setitem__ #24477
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,6 +478,52 @@ 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 (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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :)