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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

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.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.