How to Find Difference Between Dates in pandas: Quick Guide
pandas, subtract one datetime column from another to get a Timedelta object. You can then convert this difference to days, seconds, or other units using attributes like .dt.days.Quick Comparison
Here is a quick comparison of key aspects when finding date differences in pandas:
| Aspect | Subtracting Dates | Using pd.to_timedelta |
|---|---|---|
| Input Type | Datetime columns or Series | Timedelta strings or numeric values |
| Output Type | Timedelta object | Timedelta object |
| Common Use | Direct date difference | Create fixed time differences |
| Conversion | Use .dt accessor for units | Already in timedelta format |
| Example | df['date2'] - df['date1'] | pd.to_timedelta('2 days') |
Key Differences
In pandas, the most straightforward way to find the difference between two dates is by subtracting one datetime column from another. This subtraction returns a Timedelta object that represents the time difference. You can then extract the difference in days, seconds, or other units using the .dt accessor, such as .dt.days for days.
Alternatively, pandas provides the pd.to_timedelta function to create fixed time differences from strings or numbers. This is useful when you want to add or subtract a fixed duration to dates rather than comparing two date columns. The key difference is that direct subtraction compares two dates, while pd.to_timedelta creates a duration to apply.
Understanding these differences helps you choose the right method: use subtraction for comparing dates and pd.to_timedelta for fixed time intervals.
Code Comparison
Here is how to find the difference between two date columns in pandas by subtracting them:
import pandas as pd df = pd.DataFrame({ 'date1': pd.to_datetime(['2024-01-01', '2024-02-15', '2024-03-10']), 'date2': pd.to_datetime(['2024-01-05', '2024-02-20', '2024-03-15']) }) df['diff'] = df['date2'] - df['date1'] df['diff_days'] = df['diff'].dt.days print(df)
pd.to_timedelta Equivalent
Here is how to create a fixed time difference using pd.to_timedelta and add it to a date:
import pandas as pd df = pd.DataFrame({ 'date': pd.to_datetime(['2024-01-01', '2024-02-15', '2024-03-10']) }) fixed_diff = pd.to_timedelta('3 days') df['date_plus_3'] = df['date'] + fixed_diff print(df)
When to Use Which
Choose direct subtraction when you want to find the difference between two date columns in your data, such as calculating the number of days between events. Use pd.to_timedelta when you need to create or apply a fixed time duration, like adding a set number of days to a date. Both methods produce Timedelta objects but serve different practical purposes.