dataframe.rolling() function provides the ability to compute a sliding window. The concept of calculating a sliding window is most often used in signal processing and time series data. Simply put, we take a window size k at a time and do some math on it. A window of size k means k consecutive values at a time. In a very simple case, all k values have the same weight.
Syntax: DataFrame.rolling (window, min_periods = None, freq = None, center = False, win_type = None, on = None, axis = 0, closed = None)
window: Size of the moving window ... This is the number of observations used for calculating the statistic. Each window will be a fixed size. If its 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.
min_periods: Minimum number of observations in window required to have a value (otherwise result is NA). For a window that is specified by an offset, this will default to 1.
freq: Frequency to conform the data to before computing the statistic. Specified as a frequency string or DateOffset object.
center: Set the labels at the center of the window.
win_type: Provide a window type. See the notes below.
on: For a DataFrame, column on which to calculate the rolling window, rather than the index
closed: Make the interval closed on the 'right', 'left', 'both' or 'neither' endpoints. For offset-based windows, it defaults to 'right'. For fixed windows, defaults to 'both'. Remaining cases not implemented for fixed windows.
axis: int or string, default 0
Note. The freq keyword used to confirm time series data at a given frequency by resampling the data. This is done with the default parameters
resample () (that is, using the mean).
If win_type = none, then all values in the window are evenly weighted. There are various other types of sliding windows. To learn more about another type of sliding window, refer to this documentation.
To link to the CSV file used in the code, click here . This is Apple stock price data for a period of 1 year from (13-11-17) to (13-11-18)
Example # 1: a rolling sum with a size window 3 in the stock closing price column
| tr> |
Example # 2: a sliding window means a window size greater than 3. We use the default window type, which is neither alone. This way, all values will be evenly weighted.