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

0

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

Relevant Projects

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.