Python | Pandas Series.rolling()

07 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.rolling() function is a very useful function. It Provides rolling window calculations over the underlying data in the given Series object.
Syntax: Series.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)
Parameter :window : Size of the moving windowmin_periods : Minimum number of observations in window required to have a valuecenter : Set the labels at the center of the window.win_type : Provide a window type.on : str, optionalaxis : int or str, default 0closed : Make the interval closed on the ‘right’, ‘left’, ‘both’ or ‘neither’ endpoints.
Returns : a Window or Rolling sub-classed for the particular operation
Example #1: Use Series.rolling() function to find the rolling window sum of the underlying data for the given Series object. The size of the rolling window should be 2 and the weightage of each element should be same.
# importing pandas as pdimport pandas as pd  # Creating the Seriessr = pd.Series([10, 25, 3, 11, 24, 6])  # Create the Indexindex_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']  # set the indexsr.index = index_  # Print the seriesprint(sr)
Output :
Now we will use Series.rolling() function to find the sum of the underlying data having a window size of 2.
# Find sum over a window size of 2result = sr.rolling(2).sum()  # Print the returned Series objectprint(result)
Output :As we can see in the output, the Series.rolling() function has successfully returned a series object having found the sum of the underlying data over a window size of 2. Notice the first value is a missing value as there was no element previous to it so the sum could not be performed. Example #2: Use Series.rolling() function to find the rolling window sum of the underlying data for the given Series object. The size of the rolling window should be 2 and the rolling window type should be ‘triang’.
# importing pandas as pdimport pandas as pd  # Creating the Seriessr = pd.Series([10, 25, 3, 11, 24, 6])  # Create the Indexindex_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']  # set the indexsr.index = index_  # Print the seriesprint(sr)
Output :
Now we will use Series.rolling() function to find the sum of the underlying data having a window size of 2.
# Find sum over a window size of 2# We have also provided the window typeresult = sr.rolling(2, win_type ='triang').sum()  # Print the returned Series objectprint(result)
Output :
As we can see in the output, the Series.rolling() function has successfully returned a series object having found the sum of the underlying data over a window size of 2. Notice the first value is a missing value as there was no element previous to it so the sum could not be performed.
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Enumerate() in Python
Different ways to create Pandas Dataframe
How to Install PIP on Windows ?
*args and **kwargs in Python
Python OOPs Concepts
Python Classes and Objects
Introduction To PYTHON
Create a Pandas DataFrame from Lists
Python | os.path.join() method