pandas 1.4.2

NotesParametersReturnsBackRef
rolling(self, window: 'int | timedelta | BaseOffset | BaseIndexer', min_periods: 'int | None' = None, center: 'bool_t' = False, win_type: 'str | None' = None, on: 'str | None' = None, axis: 'Axis' = 0, closed: 'str | None' = None, method: 'str' = 'single')

Notes

See Windowing Operations <window.generic> for further usage details and examples.

Parameters

window : int, offset, or BaseIndexer subclass

Size of the moving window.

If an integer, the fixed number of observations used for each window.

If an offset, the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes. To learn more about the offsets & frequency strings, please see :None:None:`this link <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`.

If a BaseIndexer subclass, the window boundaries based on the defined get_window_bounds method. Additional rolling keyword arguments, namely min_periods , center , and closed will be passed to get_window_bounds .

min_periods : int, default None

Minimum number of observations in window required to have a value; otherwise, result is np.nan .

For a window that is specified by an offset, min_periods will default to 1.

For a window that is specified by an integer, min_periods will default to the size of the window.

center : bool, default False

If False, set the window labels as the right edge of the window index.

If True, set the window labels as the center of the window index.

win_type : str, default None

If None , all points are evenly weighted.

If a string, it must be a valid :None:None:`scipy.signal window function <https://docs.scipy.org/doc/scipy/reference/signal.windows.html#module-scipy.signal.windows>`.

Certain Scipy window types require additional parameters to be passed in the aggregation function. The additional parameters must match the keywords specified in the Scipy window type method signature.

on : str, optional

For a DataFrame, a column label or Index level on which to calculate the rolling window, rather than the DataFrame's index.

Provided integer column is ignored and excluded from result since an integer index is not used to calculate the rolling window.

axis : int or str, default 0

If 0 or 'index' , roll across the rows.

If 1 or 'columns' , roll across the columns.

closed : str, default None

If 'right' , the first point in the window is excluded from calculations.

If 'left' , the last point in the window is excluded from calculations.

If 'both' , the no points in the window are excluded from calculations.

If 'neither' , the first and last points in the window are excluded from calculations.

Default None ( 'right' ).

versionchanged

The closed parameter with fixed windows is now supported.

method : str {'single', 'table'}, default 'single'
versionadded

Execute the rolling operation per single column or row ( 'single' ) or over the entire object ( 'table' ).

This argument is only implemented when specifying engine='numba' in the method call.

Returns

``Window`` subclass if a ``win_type`` is passed
``Rolling`` subclass if ``win_type`` is not passed

Provide rolling window calculations.

See Also

ewm

Provides exponential weighted functions.

expanding

Provides expanding transformations.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
... df B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0

window

Rolling sum with a window length of 2 observations.

This example is valid syntax, but we were not able to check execution
>>> df.rolling(2).sum()
     B
0  NaN
1  1.0
2  3.0
3  NaN
4  NaN

Rolling sum with a window span of 2 seconds.

This example is valid syntax, but we were not able to check execution
>>> df_time = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]},
...  index = [pd.Timestamp('20130101 09:00:00'),
...  pd.Timestamp('20130101 09:00:02'),
...  pd.Timestamp('20130101 09:00:03'),
...  pd.Timestamp('20130101 09:00:05'),
...  pd.Timestamp('20130101 09:00:06')])
This example is valid syntax, but we were not able to check execution
>>> df_time
                       B
2013-01-01 09:00:00  0.0
2013-01-01 09:00:02  1.0
2013-01-01 09:00:03  2.0
2013-01-01 09:00:05  NaN
2013-01-01 09:00:06  4.0
This example is valid syntax, but we were not able to check execution
>>> df_time.rolling('2s').sum()
                       B
2013-01-01 09:00:00  0.0
2013-01-01 09:00:02  1.0
2013-01-01 09:00:03  3.0
2013-01-01 09:00:05  NaN
2013-01-01 09:00:06  4.0

Rolling sum with forward looking windows with 2 observations.

This example is valid syntax, but we were not able to check execution
>>> indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2)
... df.rolling(window=indexer, min_periods=1).sum() B 0 1.0 1 3.0 2 2.0 3 4.0 4 4.0

min_periods

Rolling sum with a window length of 2 observations, but only needs a minimum of 1 observation to calculate a value.

This example is valid syntax, but we were not able to check execution
>>> df.rolling(2, min_periods=1).sum()
     B
0  0.0
1  1.0
2  3.0
3  2.0
4  4.0

center

Rolling sum with the result assigned to the center of the window index.

This example is valid syntax, but we were not able to check execution
>>> df.rolling(3, min_periods=1, center=True).sum()
     B
0  1.0
1  3.0
2  3.0
3  6.0
4  4.0
This example is valid syntax, but we were not able to check execution
>>> df.rolling(3, min_periods=1, center=False).sum()
     B
0  0.0
1  1.0
2  3.0
3  3.0
4  6.0

win_type

Rolling sum with a window length of 2, using the Scipy 'gaussian' window type. std is required in the aggregation function.

This example is valid syntax, but we were not able to check execution
>>> df.rolling(2, win_type='gaussian').sum(std=3)
          B
0       NaN
1  0.986207
2  2.958621
3       NaN
4       NaN
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.ewm pandas.core.generic.NDFrame.expanding

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#11241
type: <class 'function'>
Commit: