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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

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.

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

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.