-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: pd.Series.shift and .diff to accept a collection of numbers #44660
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 9 commits
d6790a0
d60c2d9
a8b3a7f
5cb3f9e
b865128
7f59f5f
51830d0
9c41dce
99b7d6d
4bb6c35
bc96dbc
25fda61
381936a
8db0131
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 |
---|---|---|
|
@@ -186,6 +186,35 @@ representation of :class:`DataFrame` objects (:issue:`4889`). | |
df | ||
df.to_dict(orient='tight') | ||
|
||
.. _whatsnew_140.enhancements.shift: | ||
|
||
DataFrame.shift and Series.shift now accept an iterable for parameter ``'period'`` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :meth:`DataFrame.shift` and :meth:`Series.shift` functions can take in an iterable, such as a list, for the period parameter. When an iterable is passed | ||
to either function it returns a :class:`DataFrame` object with all of the shifted rows or columns concatenated with one another. | ||
The function applies a shift designated by each element in the iterable. The resulting :class:`DataFrame` object's columns will retain the | ||
names from the :class:`DataFrame` object that called shift, but postfixed with <name>_<num>, where name is the original | ||
column name and num correlates to the current element of the period iterable (:issue:`44424`). | ||
|
||
Usage within the :class:`DataFrame` class: | ||
.. ipython:: python | ||
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. this is fine, but the main source of information is the doc-strings which need updating with examples & prose. |
||
|
||
df = pd.DataFrame({ | ||
'a': [1, 2, 3], | ||
'b': [4, 5, 6] | ||
}) | ||
shifts = [0, 1, 2] | ||
df.shift(shifts) | ||
|
||
Usage within the :class:`Series` class: | ||
.. ipython:: python | ||
|
||
ser = pd.Series([1, 2, 3]) | ||
shifts = [0, 1, 2] | ||
|
||
ser.shift(shifts) | ||
|
||
.. _whatsnew_140.enhancements.other: | ||
|
||
Other enhancements | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5444,6 +5444,34 @@ def shift( | |
) -> DataFrame: | ||
axis = self._get_axis_number(axis) | ||
|
||
# Handle the case of multiple shifts | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if is_list_like(periods): | ||
|
||
new_df = DataFrame() | ||
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. this is superfluous |
||
|
||
from pandas.core.reshape.concat import concat | ||
|
||
for i in periods: | ||
skwirskj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not isinstance(i, int): | ||
skwirskj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError( | ||
f"Value {i} in periods is not an integer, expected an integer" | ||
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. does this have tests? |
||
) | ||
|
||
new_df = concat( | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ | ||
new_df, | ||
super() | ||
.shift(periods=i, freq=freq, axis=axis, fill_value=fill_value) | ||
.add_suffix(f"_{i}"), | ||
], | ||
axis=1, | ||
) | ||
|
||
if new_df.empty: | ||
return self | ||
|
||
return new_df | ||
|
||
ncols = len(self.columns) | ||
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0: | ||
# We will infer fill_value to match the closest column | ||
|
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.
you don't need this sentence to the end. you can say that the
suffix
parameter controls the names but don't have to fully explain here. that's the point of the doc-string.