-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Support NDFrame.shift with EAs #22387
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b29dfc6
Support NDFrame.shift with EAs
TomAugspurger c980035
Updated
TomAugspurger c4b0b97
Slice based
TomAugspurger 8d404bc
update name
TomAugspurger 64f51f4
Doc notes
TomAugspurger ab901a6
Merge remote-tracking branch 'upstream/master' into ea-shift
TomAugspurger c5b556d
Added versionadded [ci skip]
TomAugspurger 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
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 |
---|---|---|
|
@@ -59,6 +59,10 @@ class ExtensionArray(object): | |
* factorize / _values_for_factorize | ||
* argsort / _values_for_argsort | ||
|
||
The remaining methods implemented on this class should be performant, | ||
as they only compose abstract methods. Still, a more efficient | ||
implementation may be available, and these methods can be overridden. | ||
|
||
This class does not inherit from 'abc.ABCMeta' for performance reasons. | ||
Methods and properties required by the interface raise | ||
``pandas.errors.AbstractMethodError`` and no ``register`` method is | ||
|
@@ -400,6 +404,40 @@ def dropna(self): | |
|
||
return self[~self.isna()] | ||
|
||
def shift(self, periods=1): | ||
# type: (int) -> ExtensionArray | ||
""" | ||
Shift values by desired number. | ||
|
||
Newly introduced missing values are filled with | ||
``self.dtype.na_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. can you add a versionadded tag |
||
|
||
.. versionadded:: 0.24.0 | ||
|
||
Parameters | ||
---------- | ||
periods : int, default 1 | ||
The number of periods to shift. Negative values are allowed | ||
for shifting backwards. | ||
|
||
Returns | ||
------- | ||
shifted : ExtensionArray | ||
""" | ||
# Note: this implementation assumes that `self.dtype.na_value` can be | ||
# stored in an instance of your ExtensionArray with `self.dtype`. | ||
if periods == 0: | ||
return self.copy() | ||
empty = self._from_sequence([self.dtype.na_value] * abs(periods), | ||
dtype=self.dtype) | ||
if periods > 0: | ||
a = empty | ||
b = self[:-periods] | ||
else: | ||
a = self[abs(periods):] | ||
b = empty | ||
return self._concat_same_type([a, b]) | ||
|
||
def unique(self): | ||
"""Compute the ExtensionArray of unique values. | ||
|
||
|
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 |
---|---|---|
|
@@ -2068,6 +2068,18 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None, | |
limit=limit), | ||
placement=self.mgr_locs) | ||
|
||
def shift(self, periods, axis=0, mgr=None): | ||
""" | ||
Shift the block by `periods`. | ||
|
||
Dispatches to underlying ExtensionArray and re-boxes in an | ||
ExtensionBlock. | ||
""" | ||
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. bonus points for the Parameters :> (future PR ok too) |
||
# type: (int, Optional[BlockPlacement]) -> List[ExtensionBlock] | ||
return [self.make_block_same_class(self.values.shift(periods=periods), | ||
placement=self.mgr_locs, | ||
ndim=self.ndim)] | ||
|
||
|
||
class NumericBlock(Block): | ||
__slots__ = () | ||
|
@@ -2691,10 +2703,6 @@ def _try_coerce_result(self, result): | |
|
||
return result | ||
|
||
def shift(self, periods, axis=0, mgr=None): | ||
return self.make_block_same_class(values=self.values.shift(periods), | ||
placement=self.mgr_locs) | ||
|
||
def to_dense(self): | ||
# Categorical.get_values returns a DatetimeIndex for datetime | ||
# categories, so we can't simply use `np.asarray(self.values)` like | ||
|
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.
I think update in the ExtensionArray doc-string?
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.
Currently, in the class docstring we only mention the methods that either needs to implemented (because they raise AbstractMethodError otherwise) or either have a suboptimal implementation because it does the object ndarray roundtrip.
This is not the case here (which is not saying we couldn't also list other methods that can be overriden for specific reasons)