Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Use Pandas Rolling – A Simple Illustrated Guide
1
How to Use Pandas Rolling – A Simple Illustrated Guide

[Image: oZyocnFR62x1vikMg_8MVm2WHSkCFjGdTHUIzqVj...hgjIY1-KM4]

This article will demonstrate how to use a pandas dataframe method called
Code:
rolling()
.

What does the
Code:
pandas.DataFrame.rolling()
method do?


In short, it performs rolling windows calculations.

It is often used when working with time-series data or signal processing. I will shortly dive into a few practical examples to clarify what this means in practice.

The method will be given a parameter that specifies how big the window the desired calculations should be performed in.

A simple example of using time series data could be that each row of a pandas dataframe represents a day with some values.

Let’s say that the desired window size is five days. The rolling method is given a five as input, and it will perform the expected calculation based on steps of five days. 

Before an example of this, let’s see the method, its syntax, and its parameters.  

pandas.DataFrame.rolling()

Code:
Dataframe.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None, method=’single’)

Let’s dive into the parameters one by one:

window

Code:
window
:
Code:
int
,
Code:
offset
, or
Code:
BaseIndexer
subclass


This is the size of the moving window.

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

If an offset, the time period of each window. Each window will be variable-sized based on the observations included in the time period. This is only valid for
Code:
datetime
-like indexes.

If a
Code:
BaseIndexer
subclass, the window boundaries are based on the defined
Code:
get_window_bounds()
method. Additional rolling keywords argument, namely
Code:
min_periods
,
Code:
center
, and
Code:
closed
will be passed to
Code:
get_window_bounds()
.

min_periods

Code:
min_periods
:
Code:
int
, default
Code:
None


This is the minimum number of observations in the window required to have a value.

Otherwise, the result is assigned
Code:
np.nan
.
  • For a window that is specified by an offset,
    Code:
    min_periods
    will default to 1.

  • For a window specified by an integer,
    Code:
    min_periods
    will default to the size of the window. 

center

Code:
center
:
Code:
bool
, default
Code:
False


If
Code:
False
, set the window labels as the right edge of the window index. If
Code:
True
, set the window labels as the center of the window index. 

win_type

Code:
win_type
:
Code:
str
, default
Code:
None


If
Code:
None
, all points are evenly weighted.

If a string, it must be a valid window function from
Code:
scipy.signal
. 

Some of the 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

Code:
on
:
Code:
str
, optional


For a Dataframe, a column label or index level on which to calculate the rolling window, rather than the Dataframes index.

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

axis

Code:
axis
:
Code:
int
or
Code:
str
, default 0


If 0 or
Code:
'index'
, roll across the rows. If 1 or
Code:
'columns'
, roll across the columns. 

closed

Code:
closed
:
Code:
str
, default
Code:
None

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

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

  • If
    Code:
    'both'
    , then no points in the window are excluded from the calculations.

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

Default
Code:
None
means
Code:
'right'
.

method

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


Execute the rolling operation per single column or row for
Code:
'single'
or over the entire object for
Code:
'table'
.

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

This part was obtained from the official pandas documentation. 

Data

The data I will be working with for this tutorial is historical data for a stock, the amazon stock.

I use the python package
Code:
yfinance
to import the data. I will use data starting from
Code:
2021-04-01
and running one year forward in time.

The data only includes trading days, i.e., days when the stock market was open.

Code:
# Get the stock data from Yahoo finance AmazonData1y = yfinance.Ticker("AMZN").history(period='1y', actions=False, end='2022-04-01') display(AmazonData1y.head(20))

[Image: zvwb_9G7ndEgIGlYaImPZXbTlNp-XAHIhp9RfR7z...hz8XymsCTv]

The resulting dataframe contains data about the opening price, the highest price, the lowest price, the closing price, and the trading volume for each day. 

Calculating moving averages

The first calculations using the rolling method I will do are some different moving averages values. They are often applied in stock analysis.

[Image: 1f4a1.png] A moving average value is a statistic that captures the average change in a data series over time. (source)

Let’s calculate the moving averages for seven days and 15 days for the stock closing price and add those values as new columns to the existing amazon dataframe.

They are named
Code:
'MA7'
and
Code:
'MA15'
.

Code:
# Calculating the 7 and 15 day moving averages based on closing price # and adding them as new columns AmazonData1y['7MA'] = AmazonData1y['Close'].rolling(7).mean() AmazonData1y['15MA'] = AmazonData1y['Close'].rolling(15).mean() display(AmazonData1y.head(20))

[Image: clZ14KnZ_PAwayYCoeaWT16QUiQCelMHYdf7uF6d...R9TNf6Ab_p]

Since there is no data before
Code:
2021-04-01
, no seven-day moving average can be calculated before
Code:
2021-04-13
and no 15-day moving average before
Code:
2021-04-23
. 

Calculating the Sum of Trading Volume

Let’s now instead use the rolling method to calculate the sum of the volume from the last five trading days to spot if there was any spike in volume.

It is done in the same way as for the moving average, but here the
Code:
sum()
method is used together with the rolling method instead of the
Code:
mean()
method.

I will also add this as a new column to the existing Amazon dataframe. 

Code:
# Calculating 5 day volume using rolling AmazonData1y['5VOL'] = AmazonData1y['Volume'].rolling(5).sum() display(AmazonData1y.head(20))

[Image: rSd107FYEgpg88bQgfdDZpvSn942PyuygydvwdAQ...v9krkDrxhQ]

This metric might not be the most useful but it is a good way to explain how you could use the rolling method together with the
Code:
sum()
method. 

Using rolling() with Aggregation

If combining the
Code:
rolling()
method with the aggregation method
Code:
agg()
, it is easy to perform rolling calculations on multiple columns simultaneously.

Say that I would like to find the highest high and the lowest low for the last seven days. 

Code:
# Performing rolling calculations on multiple columns at the # same time using .agg() SevenHighAndLow = AmazonData1y.rolling(7).agg({'High': 'max', 'Low': 'min'}) display(SevenHighAndLow.head(20))

[Image: UtKxjPzoAASwzbcdDubHkvW2bV9n-3T3ZOGdLCR6...Z7Ifp5wnpW]

Plotting the Values

This part will be included to visualize the value calculated. It’s a bit more appealing than simply just looking at columns of a dataframe. 

First, let’s plot the calculated moving averages values alongside the closing price. 

Code:
# Plotting the closing price with the 7 and 15 day moving averages AmazonData1y.plot(y=['Close', '7MA', '15MA'], kind='line', figsize=(14,12)) plt.title('Closing price, 7MA and 15MA', fontsize=16) plt.xlabel('Date') plt.ylabel('Stock price($)') plt.show()

[Image: jQSp1ogckMdep922IJMkK3PKa6ENu1-o8GWNm2g0...c5IRgY2_p1]

And then the accumulated 5 day volume alongside the closing price. 

Code:
# Plotting the closing price alongside the 5 day volume AmazonData1y.plot(y=['Close', '5VOL'], secondary_y='5VOL', kind='line', ylabel='Stock Price ($)', figsize=(14,12)) plt.title('Closing price and 5 day accumulated volume', fontsize=16) plt.xlabel('Date') plt.ylabel('Volume') plt.show()

[Image: 83qk16yeQwY9zCJJY3w2KbOIvQWZzCX2RjCjUHZt...dDscI5IaEB]

Summary

This was a short tutorial on applying the
Code:
rolling()
method on a pandas dataframe using some statistics.

The goal of this article was to demonstrate some simple examples of how the
Code:
rolling()
method works, and I hope that it did accomplish that goal.

The
Code:
rolling()
method can be used for most statistics calculations, so try and explore it using other methods than those used for this article. 
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: