
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Comparing Timestamp in Python – Pandas
To compare timestamps, we can use index operator i.e. the square brackets. At first, import the required library −
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"), ] })
Let us now compare some timestamps from both the date columns −
timestamp1_diff = abs(dataFrame['Date_of_Purchase'][0]-dataFrame['Date_of_Service'][0]) timestamp2_diff = abs(dataFrame['Date_of_Purchase'][1]-dataFrame['Date_of_Service'][1])
Compare all the timestamps −
timestamp_diff = abs(dataFrame['Date_of_Purchase']-dataFrame['Date_of_Service'])
Example
Following is the complete 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 # compare specific timestamps timestamp1_diff = abs(dataFrame['Date_of_Purchase'][0]-dataFrame['Date_of_Service'][0]) timestamp2_diff = abs(dataFrame['Date_of_Purchase'][1]-dataFrame['Date_of_Service'][1]) print"\nDifference between Car 1 Date of Purchase and Service \n",timestamp1_diff print"\nDifference between Car 2 Date of Purchase and Service \n",timestamp2_diff # compare all timestamps by finding difference 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 Car 1 Date of Purchase and Service 153 days 00:00:00 Difference between Car 2 Date of Purchase and Service 153 days 00:00:00 Difference between two Timestamps: 0 153 days 1 153 days 2 153 days 3 153 days 4 153 days dtype: timedelta64[ns]
- Related Articles
- Python Pandas - Convert given Timestamp to Period
- Python Pandas - Convert PeriodIndex object to Timestamp
- Python - Get the weekday from Timestamp object in Pandas
- Python Pandas - Convert Timestamp to another time zone
- Python Pandas - Convert naive Timestamp to local time zone
- Python Pandas - Convert given Timestamp to Period with minutely frequency
- Python Pandas - Convert given Timestamp to Period with weekly frequency
- Python Pandas - Convert given Timestamp to Period with monthly frequency
- Python Pandas - Convert given Timestamp to Period with quarterly frequency
- Python Pandas - Convert given Timestamp to Period with hourly frequency
- Python Pandas - Return the Timestamp representation of the Period object
- Comparing dates in Python
- Pandas - Convert a Timestamp object to a native Python datetime object
- Python Pandas - Get the current date and time from Timestamp object
- Python Pandas - Construct a naive UTC datetime from a POSIX timestamp

Advertisements