DataFrame.rolling

函数定义

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, 
on=None, axis=0, closed=None, method='single')

Provide rolling window calculations.

函数参数

  • window:int, offset, or BaseIndexer subclass
    Size of the moving window.

    • int This is the number of observations used for calculating the statistic. Each window will be a fixed size.
    • offset If it's an offset then this will be 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.
    • BaseIndexer subclass If a BaseIndexer subclass is passed, calculates 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 NA). For a window that is specified by an offset, min_periods will default to 1. Otherwise, min_periods will default to the size of the window.

  • center:bool, default False
    Set the labels at the center of the window.

  • win_type:str, default None
    Provide a window type. If None, all points are evenly(相等地,均匀地) weighted. See the notes below for further information.

  • on:str, optional
    For a DataFrame, a datetime-like column 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

  • closed:str, default None
    Make the interval(区间) closed on the ‘right’, ‘left’, ‘both’ or ‘neither’ endpoints. Defaults to ‘right’.

  • method:str {‘single’, ‘table’}, default ‘single’
    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: a Window or Rolling sub-classed for the particular operation

  • By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting center=True.

  • To learn more about the offsets & frequency strings, please see this link.

  • If win_type=None, all points are evenly weighted; otherwise, win_type can accept a string of any scipy.signal window function.

  • 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. Please see the third example below on how to add the additional parameters.

例子

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
df.rolling(2, win_type='triang').sum()
     B
0  NaN
1  0.5
2  1.5
3  NaN
4  NaN
df.rolling(2, win_type='gaussian').sum(std=3)
          B
0       NaN
1  0.986207
2  2.958621
3       NaN
4       NaN
df.rolling(2).sum()
     B
0  NaN
1  1.0
2  3.0
3  NaN
4  NaN
df.rolling(2, min_periods=1).sum()
     B
0  0.0
1  1.0
2  3.0
3  2.0
4  4.0

Same as above, but with forward-looking windows

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

A ragged (meaning not-a-regular frequency), time-indexed DataFrame

df = 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')])
df
                       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
df.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

我们看一下closed参数的使用。
image
image

默认情况下,参数closed='right',
image

下面我们将参数closed设置为closed='left',
image

posted on 2021-12-12 22:46  朴素贝叶斯  阅读(555)  评论(0编辑  收藏  举报

导航