How to deal with Rolling Time Window in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to deal with Rolling Time Window in Python?

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.

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

Relevant Projects

Machine learning for Retail Price Recommendation with Python
Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.