Skip to content

Commit bcd76ed

Browse files
wandersoncferreirajorisvandenbossche
wandersoncferreira
authored andcommitted
DOC: add section on groupby().rolling/expanding/resample (#14801)
1 parent dc23751 commit bcd76ed

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

doc/source/computation.rst

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ computing common *window* or *rolling* statistics. Among these are count, sum,
214214
mean, median, correlation, variance, covariance, standard deviation, skewness,
215215
and kurtosis.
216216

217+
Starting in version 0.18.1, the ``rolling()`` and ``expanding()``
218+
functions can be used directly from DataFrameGroupBy objects,
219+
see the :ref:`groupby docs <groupby.transform.window_resample>`.
220+
221+
217222
.. note::
218223

219224
The API for window statistics is quite similar to the way one works with ``GroupBy`` objects, see the documentation :ref:`here <groupby>`

doc/source/groupby.rst

+48
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,54 @@ and that the transformed data contains no NAs.
614614
615615
grouped.ffill()
616616
617+
618+
.. _groupby.transform.window_resample:
619+
620+
New syntax to window and resample operations
621+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
622+
.. versionadded:: 0.18.1
623+
624+
Working with the resample, expanding or rolling operations on the groupby
625+
level used to require the application of helper functions. However,
626+
now it is possible to use ``resample()``, ``expanding()`` and
627+
``rolling()`` as methods on groupbys.
628+
629+
The example below will apply the ``rolling()`` method on the samples of
630+
the column B based on the groups of column A.
631+
632+
.. ipython:: python
633+
634+
df = pd.DataFrame({'A': [1] * 10 + [5] * 10,
635+
'B': np.arange(20)})
636+
df
637+
638+
df.groupby('A').rolling(4).B.mean()
639+
640+
641+
The ``expanding()`` method will accumulate a given operation
642+
(``sum()`` in the example) for all the members of each particular
643+
group.
644+
645+
.. ipython:: python
646+
647+
df.groupby('A').expanding().sum()
648+
649+
650+
Suppose you want to use the ``resample()`` method to get a daily
651+
frequency in each group of your dataframe and wish to complete the
652+
missing values with the ``ffill()`` method.
653+
654+
.. ipython:: python
655+
656+
df = pd.DataFrame({'date': pd.date_range(start='2016-01-01',
657+
periods=4,
658+
freq='W'),
659+
'group': [1, 1, 2, 2],
660+
'val': [5, 6, 7, 8]}).set_index('date')
661+
df
662+
663+
df.groupby('group').resample('1D').ffill()
664+
617665
.. _groupby.filter:
618666

619667
Filtration

doc/source/timeseries.rst

+3
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,9 @@ limited to, financial applications.
12871287

12881288
``.resample()`` is a time-based groupby, followed by a reduction method on each of its groups.
12891289

1290+
Starting in version 0.18.1, the ``resample()`` function can be used directly from
1291+
DataFrameGroupBy objects, see the :ref:`groupby docs <groupby.transform.window_resample>`.
1292+
12901293
.. note::
12911294

12921295
``.resample()`` is similar to using a ``.rolling()`` operation with a time-based offset, see a discussion `here <stats.moments.ts-versus-resampling>`

0 commit comments

Comments
 (0)