Skip to content

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

Merged
merged 5 commits into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions doc/source/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ computing common *window* or *rolling* statistics. Among these are count, sum,
mean, median, correlation, variance, covariance, standard deviation, skewness,
and kurtosis.

Now the ``rolling()`` and ``expanding()`` functions can be used directly from
Copy link
Contributor

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

DataFrameGroupBy objects, see :ref:`whatsnew docs <whatsnew_0181.deferred_ops>` and :ref:`groupby transformation <groupby.transform.window_resample>`


.. note::

The API for window statistics is quite similar to the way one works with ``GroupBy`` objects, see the documentation :ref:`here <groupby>`
Expand Down
67 changes: 67 additions & 0 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)})

Copy link
Contributor

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The 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()``:

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
5 changes: 5 additions & 0 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The 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>`
Expand Down Expand Up @@ -1353,6 +1356,8 @@ retains the input representation.
frequency periods.




Up Sampling
~~~~~~~~~~~

Expand Down