How to deal with Rolling Time Window in Python?

This recipe helps you deal with Rolling Time Window in Python

Recipe Objective

While doing statical analysis on any dataset we need to calculate various statical measure in various form. Have you ever tried to calculate any measure for a specific numbers of rows and then moving to another set of row by increasing the index value of every row by one. It will give us the statical measure for every set of data and by this we can get the idea that how the measure is changing with the rows. This can be done by rolling function.

Access House Price Prediction Project using Machine Learning with Source Code  

This python source code does the following :
1. Creates your own time series data.
2. Adding new columns to datagram
3. Finds mean and max for rolling window

So this is the recipe on how we can deal with Rolling Time Window in Python.

Step 1 - Import the library

import pandas as pd

We have only imported Pandas which is needed.

Step 2 - Setting up the Data

We have created an array of date by using the function date_range in which we have passed the initial date, period and the frequency as weekly. Then we have passed it through pd.DataFrame as a index to create a dataframe. We have added another feature in the data frame named as 'Stock_Price'. time_index = pd.date_range('21/09/2020', periods=6, freq='W') df = pd.DataFrame(index=time_index) df['Stock_Price'] = [100,200,300,400,500,600] print(df)

Step 3 - Creating A Rolling Time Window

So here we have used rolling function with parameter window which signifies the number of rows the function will select to compute the statical measure. We have created two functions one will calculate the mean and other will calculate the max of all the rows which will be selected. df1 = df.rolling(window=3).mean() print(df1) df2 = df.rolling(window=3).max() print(df2) So the output comes as

            Stock_Price
2020-09-27          100
2020-10-04          200
2020-10-11          300
2020-10-18          400
2020-10-25          500
2020-11-01          600

            Stock_Price
2020-09-27          NaN
2020-10-04          NaN
2020-10-11        200.0
2020-10-18        300.0
2020-10-25        400.0
2020-11-01        500.0

            Stock_Price
2020-09-27          NaN
2020-10-04          NaN
2020-10-11        300.0
2020-10-18        400.0
2020-10-25        500.0
2020-11-01        600.0

Download Materials

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.