Python | Pandas dataframe.rolling ()

Python Methods and Functions

The Pandas 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

# import pandas as pd

import pandas as pd

# By default the "date" column was in string format
# we need to convert it to date and time format
# parse_dates = [& quot; date & quot;], converts the column "date" to date and time format

# Resampling only works with time series data
# so we convert the column "date" to index
# index_col = & quot; date & quot ;, creates a column & quot; date & quot;

df = pd.read_csv ( "apple.csv" , parse_dates = [ "date" ], index_col = "date" )

# Print the first 10 lines data

df [: 10 ]

No. 3 specify t the size of the window
# we selected a triangle type window
# which returns a triangle type window

The # sum () function finds the amount for
# all windows in our data frame

df.close.rolling ( 3 , win_type = ' triang' ). sum ()


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.

# import pandas as pd

import pandas as pd


df = pd.read_csv ( "apple .csv " , parse_dates = [ "date" ], index_col = "date " )

# close - the column by which
# we are performing the operation
The # mean () function finds the average meaning f for each window

df.close.rolling ( 3 ). mean ()