-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: add section on groupby().rolling/expanding/resample #14801
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 2 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 |
---|---|---|
|
@@ -614,6 +614,73 @@ and that the transformed data contains no NAs. | |
|
||
grouped.ffill() | ||
|
||
|
||
.. _groupby.transform.window_resample: | ||
|
||
New syntax to window and resample operations | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
.. versionadded:: 0.18.1 | ||
|
||
Working with the resample, expanding or rolling operations on the groupby | ||
level used to require the application of helper functions. However, | ||
now it is possible to use ``resample()``, ``expanding()`` and | ||
``rolling()`` as methods on groupbys. We will find below simple | ||
examples of the application of each of the refered methods: | ||
|
||
|
||
Example of the ``rolling()`` method applied to groupbys: | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'A': [1] * 20 + [2] * 12, | ||
'B': np.arange(32)}) | ||
|
||
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. add a line showing df |
||
df.groupby('A').rolling(4).B.mean() | ||
|
||
.. note:: | ||
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. you don't need the note |
||
|
||
The example above is grouping the values of the column A, creating | ||
a rolling window of size 4 on the samples of columns B and applying | ||
the average. | ||
|
||
|
||
Example of the ``resample()``: | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'date': pd.date_range(start='2016-01-01', | ||
periods=4, | ||
freq='W'), | ||
'group': [1, 1, 2, 2], | ||
'val': [5, 6, 7, 8]}).set_index('date') | ||
|
||
df.groupby('group').resample('1D').ffill() | ||
|
||
.. note:: | ||
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. remove the note, show df (as its different from above). |
||
|
||
The example above is grouping the values of column ``group``, | ||
applying a resample operation to a daily frequency and completing | ||
the missing values with the ``ffill()`` method. | ||
|
||
|
||
|
||
Example of the ``expanding()``: | ||
|
||
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. move this after rolling (and re-use the same df) |
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'group': [1] * 5 + [2] * 5, | ||
'val': [5] * 5 + [1] * 5 }) | ||
|
||
df.groupby('group').expanding().sum() | ||
|
||
.. note:: | ||
|
||
The example above of the ``expanding`` method is grouping the | ||
dataframe into the groups available at the columns groups. The | ||
expanding operation will accumulate the defined operations (sum() | ||
in this example) for all the members of the a particular group. | ||
|
||
|
||
.. _groupby.filter: | ||
|
||
Filtration | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1288,6 +1288,9 @@ limited to, financial applications. | |
``.resample()`` is a time-based groupby, followed by a reduction method on each of its groups. | ||
See some :ref:`cookbook examples <cookbook.resample>` for some advanced strategies | ||
|
||
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. starting in 0.18.1 |
||
Now the ``resample()`` function can be used directly from | ||
DataFrameGroupBy objects, see :ref:`whatsnew docs <whatsnew_0181.deferred_ops>` and :ref:`groupby transformation <groupby.transform.window_resample>` | ||
|
||
.. note:: | ||
|
||
``.resample()`` is similar to using a ``.rolling()`` operation with a time-based offset, see a discussion :ref:`here <stats.moments.ts-versus-resampling>` | ||
|
@@ -1353,6 +1356,8 @@ retains the input representation. | |
frequency periods. | ||
|
||
|
||
|
||
|
||
Up Sampling | ||
~~~~~~~~~~~ | ||
|
||
|
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.
say starting in 0.18.1