Skip to content

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
28 changes: 28 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5444,6 +5444,34 @@ def shift(
) -> DataFrame:
axis = self._get_axis_number(axis)

# Handle the case of multiple shifts
if is_list_like(periods):

new_df = DataFrame()
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
if not isinstance(i, int):
raise TypeError(
f"Value {i} in periods is not an integer, expected an integer"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have tests?

)

new_df = concat(
[
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
Expand Down
Loading