- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding difference in Timestamps – Python Pandas
To find the difference in timestamps, we can use index operator i.e. the square brackets to find the difference. For timestamps, we need to also use abs(). At first, import the required library −
import pandas as pd
Create a DataFrame with 3 columns. We have two date columns with timestamp −
dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), pd.Timestamp("2021-06-29"), pd.Timestamp("2021-03-20"), ], "Date_of_Service": [ pd.Timestamp("2021-11-10"), pd.Timestamp("2021-12-11"), pd.Timestamp("2021-11-25"), pd.Timestamp("2021-11-29"), pd.Timestamp("2021-08-20"), ] })
Let us now find the difference between timestamps from both the date columns −
timestamp_diff = abs(dataFrame['Date_of_Purchase']-dataFrame['Date_of_Service'])
Example
Following is the code −
import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), pd.Timestamp("2021-06-29"), pd.Timestamp("2021-03-20"), ], "Date_of_Service": [ pd.Timestamp("2021-11-10"), pd.Timestamp("2021-12-11"), pd.Timestamp("2021-11-25"), pd.Timestamp("2021-11-29"), pd.Timestamp("2021-08-20"), ] }) print"DataFrame...\n", dataFrame # find difference in timestamps timestamp_diff = abs(dataFrame['Date_of_Purchase']-dataFrame['Date_of_Service']) print"\nDifference between two Timestamps: \n",timestamp_diff
Output
This will produce the following output −
DataFrame... Car Date_of_Purchase Date_of_Service 0 Audi 2021-06-10 2021-11-10 1 Lexus 2021-07-11 2021-12-11 2 Tesla 2021-06-25 2021-11-25 3 Mercedes 2021-06-29 2021-11-29 4 BMW 2021-03-20 2021-08-20 Difference between two Timestamps: 0 153 days 1 153 days 2 153 days 3 153 days 4 153 days dtype: timedelta64[ns]
- Related Articles
- Compare specific Timestamps for a Pandas DataFrame – Python
- Python program to find difference between two timestamps
- What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?
- Python Pandas - Create a time interval and use Timestamps as the bounds
- MySQL difference between two timestamps in Seconds?
- Difference between two timestamps in seconds in MySQL?
- Python Pandas - Finding the uncommon rows between two DataFrames
- Get the difference between two timestamps in seconds in MySQL?
- How to get time difference between two timestamps in seconds?
- Find the difference between two timestamps in days with MySQL
- Difference between data frames and matrices in Python Pandas?
- Why do I get different timestamps in python on different machines?
- Python Pandas – Find the Difference between two Dataframes
- Finding the length of words in a given Pandas series
- How to compare timestamps in MySQL?

Advertisements