How to calculate difference between Dates in Python?

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

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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

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

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

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

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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.

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.