How to introduce LAG time in Python?

This recipe helps you introduce LAG time in Python

Recipe Objective

Have you ever tried to shift the datetime to create a lag between data and datetime.

So this is the recipe on we can introduce LAG time in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which is needed.

Step 2 - Setting up the Data

We have created a dataset by making features and assining values to them. We have used date_range function to create a datetime dataset with frequency as Weekly. df = pd.DataFrame() df["dates"] = pd.date_range("11/11/2016", periods=5, freq="W") df["stock_price"] = [1.1,2.2,3.3,4.4,5.5]

Step 3 - Creating Lag in data

For better understanding we are first creating a lag of 1 unit and then a lag of 2 unit. We cah do this by shift function. df["previous_days_stock_price"] = df["stock_price"].shift(1) print(df) df["previous_days_stock_price"] = df["stock_price"].shift(2) print(df) So the output comes as

       dates  stock_price  previous_days_stock_price
0 2016-11-13          1.1                        NaN
1 2016-11-20          2.2                        1.1
2 2016-11-27          3.3                        2.2
3 2016-12-04          4.4                        3.3
4 2016-12-11          5.5                        4.4

       dates  stock_price  previous_days_stock_price
0 2016-11-13          1.1                        NaN
1 2016-11-20          2.2                        NaN
2 2016-11-27          3.3                        1.1
3 2016-12-04          4.4                        2.2
4 2016-12-11          5.5                        3.3

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

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.

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.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.