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?

This recipe helps you calculate difference between Dates in Python

0

## 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
```

#### Relevant Projects

##### 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.

##### 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.

##### 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.

##### 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.

##### 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.

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

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

##### Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

##### 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.