Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Compare specific Timestamps for a Pandas DataFrame – Python
To compare specific timestamps in a Pandas DataFrame, you can access individual rows using index numbers and calculate the difference between timestamp columns. This is useful for analyzing time intervals between related events.
Creating a DataFrame with Timestamps
First, let's create a DataFrame containing timestamp data ?
import pandas as pd
# Create a DataFrame with timestamp 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-05"),
pd.Timestamp("2021-12-03"),
pd.Timestamp("2021-10-30"),
pd.Timestamp("2021-11-29"),
pd.Timestamp("2021-08-20"),
]
})
print("DataFrame...")
print(dataFrame)
DataFrame...
Car Date_of_Purchase Date_of_Service
0 Audi 2021-06-10 2021-11-05
1 Lexus 2021-07-11 2021-12-03
2 Tesla 2021-06-25 2021-10-30
3 Mercedes 2021-06-29 2021-11-29
4 BMW 2021-03-20 2021-08-20
Comparing Specific Timestamps
Use index numbers in square brackets to access specific rows and calculate timestamp differences ?
import pandas as pd
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-05"),
pd.Timestamp("2021-12-03"),
pd.Timestamp("2021-10-30"),
pd.Timestamp("2021-11-29"),
pd.Timestamp("2021-08-20"),
]
})
# Compare specific timestamps using index positions
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])
timestamp3_diff = abs(dataFrame['Date_of_Purchase'][2] - dataFrame['Date_of_Service'][2])
print("Difference between Car 1 Date of Purchase and Service:")
print(timestamp1_diff)
print("\nDifference between Car 2 Date of Purchase and Service:")
print(timestamp2_diff)
print("\nDifference between Car 3 Date of Purchase and Service:")
print(timestamp3_diff)
Difference between Car 1 Date of Purchase and Service: 148 days 00:00:00 Difference between Car 2 Date of Purchase and Service: 145 days 00:00:00 Difference between Car 3 Date of Purchase and Service: 127 days 00:00:00
Using iloc for More Flexible Access
You can also use iloc for position-based indexing ?
import pandas as pd
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-05"),
pd.Timestamp("2021-12-03"),
pd.Timestamp("2021-10-30"),
pd.Timestamp("2021-11-29"),
pd.Timestamp("2021-08-20"),
]
})
# Using iloc for position-based access
for i in range(3):
purchase_date = dataFrame['Date_of_Purchase'].iloc[i]
service_date = dataFrame['Date_of_Service'].iloc[i]
car_name = dataFrame['Car'].iloc[i]
diff = abs(service_date - purchase_date)
print(f"{car_name}: {diff}")
Audi: 148 days 00:00:00 Lexus: 145 days 00:00:00 Tesla: 127 days 00:00:00
Conclusion
Use square bracket indexing or iloc to access specific timestamp rows. The abs() function ensures positive differences regardless of which date is earlier. This approach is perfect for comparing individual timestamp pairs in your DataFrame.
Advertisements
