Skip to content

Commit f9ce6f0

Browse files
authored
DOC: Add rolling in reference/groupby.rst (#51119)
* Add rolling in reference/groupby.rst * Add more docstring * Add see also * Change See also * Fix ci * Fix ci * Remove substituion
1 parent 0170fa0 commit f9ce6f0

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

doc/source/reference/groupby.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Function application
9797
DataFrameGroupBy.quantile
9898
DataFrameGroupBy.rank
9999
DataFrameGroupBy.resample
100+
DataFrameGroupBy.rolling
100101
DataFrameGroupBy.sample
101102
DataFrameGroupBy.sem
102103
DataFrameGroupBy.shift
@@ -152,6 +153,7 @@ Function application
152153
SeriesGroupBy.quantile
153154
SeriesGroupBy.rank
154155
SeriesGroupBy.resample
156+
SeriesGroupBy.rolling
155157
SeriesGroupBy.sample
156158
SeriesGroupBy.sem
157159
SeriesGroupBy.shift

pandas/core/groupby/groupby.py

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,11 +2650,134 @@ def resample(self, rule, *args, **kwargs):
26502650
return get_resampler_for_grouping(self, rule, *args, **kwargs)
26512651

26522652
@final
2653-
@Substitution(name="groupby")
2654-
@Appender(_common_see_also)
26552653
def rolling(self, *args, **kwargs) -> RollingGroupby:
26562654
"""
26572655
Return a rolling grouper, providing rolling functionality per group.
2656+
2657+
Parameters
2658+
----------
2659+
window : int, timedelta, str, offset, or BaseIndexer subclass
2660+
Size of the moving window.
2661+
2662+
If an integer, the fixed number of observations used for
2663+
each window.
2664+
2665+
If a timedelta, str, or offset, the time period of each window. Each
2666+
window will be a variable sized based on the observations included in
2667+
the time-period. This is only valid for datetimelike indexes.
2668+
To learn more about the offsets & frequency strings, please see `this link
2669+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__.
2670+
2671+
If a BaseIndexer subclass, the window boundaries
2672+
based on the defined ``get_window_bounds`` method. Additional rolling
2673+
keyword arguments, namely ``min_periods``, ``center``, ``closed`` and
2674+
``step`` will be passed to ``get_window_bounds``.
2675+
2676+
min_periods : int, default None
2677+
Minimum number of observations in window required to have a value;
2678+
otherwise, result is ``np.nan``.
2679+
2680+
For a window that is specified by an offset,
2681+
``min_periods`` will default to 1.
2682+
2683+
For a window that is specified by an integer, ``min_periods`` will default
2684+
to the size of the window.
2685+
2686+
center : bool, default False
2687+
If False, set the window labels as the right edge of the window index.
2688+
2689+
If True, set the window labels as the center of the window index.
2690+
2691+
win_type : str, default None
2692+
If ``None``, all points are evenly weighted.
2693+
2694+
If a string, it must be a valid `scipy.signal window function
2695+
<https://docs.scipy.org/doc/scipy/reference/signal.windows.html#module-scipy.signal.windows>`__.
2696+
2697+
Certain Scipy window types require additional parameters to be passed
2698+
in the aggregation function. The additional parameters must match
2699+
the keywords specified in the Scipy window type method signature.
2700+
2701+
on : str, optional
2702+
For a DataFrame, a column label or Index level on which
2703+
to calculate the rolling window, rather than the DataFrame's index.
2704+
2705+
Provided integer column is ignored and excluded from result since
2706+
an integer index is not used to calculate the rolling window.
2707+
2708+
axis : int or str, default 0
2709+
If ``0`` or ``'index'``, roll across the rows.
2710+
2711+
If ``1`` or ``'columns'``, roll across the columns.
2712+
2713+
For `Series` this parameter is unused and defaults to 0.
2714+
2715+
closed : str, default None
2716+
If ``'right'``, the first point in the window is excluded from calculations.
2717+
2718+
If ``'left'``, the last point in the window is excluded from calculations.
2719+
2720+
If ``'both'``, the no points in the window are excluded from calculations.
2721+
2722+
If ``'neither'``, the first and last points in the window are excluded
2723+
from calculations.
2724+
2725+
Default ``None`` (``'right'``).
2726+
2727+
method : str {'single', 'table'}, default 'single'
2728+
Execute the rolling operation per single column or row (``'single'``)
2729+
or over the entire object (``'table'``).
2730+
2731+
This argument is only implemented when specifying ``engine='numba'``
2732+
in the method call.
2733+
2734+
Returns
2735+
-------
2736+
RollingGroupby
2737+
Return a new grouper with our rolling appended.
2738+
2739+
See Also
2740+
--------
2741+
Series.rolling : Calling object with Series data.
2742+
DataFrame.rolling : Calling object with DataFrames.
2743+
Series.groupby : Apply a function groupby to a Series.
2744+
DataFrame.groupby : Apply a function groupby.
2745+
2746+
Examples
2747+
--------
2748+
>>> df = pd.DataFrame({'A': [1, 1, 2, 2],
2749+
... 'B': [1, 2, 3, 4],
2750+
... 'C': [0.362, 0.227, 1.267, -0.562]})
2751+
>>> df
2752+
A B C
2753+
0 1 1 0.362
2754+
1 1 2 0.227
2755+
2 2 3 1.267
2756+
3 2 4 -0.562
2757+
2758+
>>> df.groupby('A').rolling(2).sum()
2759+
B C
2760+
A
2761+
1 0 NaN NaN
2762+
1 3.0 0.589
2763+
2 2 NaN NaN
2764+
3 7.0 0.705
2765+
2766+
>>> df.groupby('A').rolling(2, min_periods=1).sum()
2767+
B C
2768+
A
2769+
1 0 1.0 0.362
2770+
1 3.0 0.589
2771+
2 2 3.0 1.267
2772+
3 7.0 0.705
2773+
2774+
>>> df.groupby('A').rolling(2, on='B').sum()
2775+
B C
2776+
A
2777+
1 0 1 NaN
2778+
1 2 0.589
2779+
2 2 3 NaN
2780+
3 4 0.705
26582781
"""
26592782
from pandas.core.window import RollingGroupby
26602783

0 commit comments

Comments
 (0)