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

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

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.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

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.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

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

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.

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