How to calculate difference between Dates in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to calculate difference between Dates in Python?

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.

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

Relevant Projects

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.

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

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.