How to calculate difference between Dates in Python?

This recipe helps you calculate difference between Dates in Python

Recipe Objective

Have you worked on dates in python? Have you tried to find difference between the dates?

So this is the recipe on how we can calculate difference between Dates in Python.

Build a Multi Touch Attribution Model in Python with Source Code

Step 1 - Import the library

import pandas as pd

We have only imported pandas which is needed.

Step 2 - Creating a Date Frame

We have created a data frame with Time stamp on which we will perform the operation. df = pd.DataFrame() df["Date1"] = [pd.Timestamp("01-01-2017"), pd.Timestamp("01-04-2017")] df["Date2"] = [pd.Timestamp("01-01-2017"), pd.Timestamp("01-06-2017")]

Step 3 - Finding Difference

We are using two methods for better understanding, one by directly finding the difference and another by using delta function on the two features. print(df["Date2"] - df["Date1"]) print(pd.Series(delta.days for delta in (df["Date2"] - df["Date1"]))) So the output comes as

0   0 days
1   2 days
dtype: timedelta64[ns]
0    0
1    2
dtype: int64

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 a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

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.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

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

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.