0
0
PandasComparisonBeginner · 3 min read

How to Find Difference Between Dates in pandas: Quick Guide

To find the difference between dates in 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:

AspectSubtracting DatesUsing pd.to_timedelta
Input TypeDatetime columns or SeriesTimedelta strings or numeric values
Output TypeTimedelta objectTimedelta object
Common UseDirect date differenceCreate fixed time differences
ConversionUse .dt accessor for unitsAlready in timedelta format
Exampledf['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:

python
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)
Output
date1 date2 diff diff_days 0 2024-01-01 2024-01-05 4 days 4 1 2024-02-15 2024-02-20 5 days 5 2 2024-03-10 2024-03-15 5 days 5
↔️

pd.to_timedelta Equivalent

Here is how to create a fixed time difference using pd.to_timedelta and add it to a date:

python
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)
Output
date date_plus_3 0 2024-01-01 2024-01-04 1 2024-02-15 2024-02-18 2 2024-03-10 2024-03-13
🎯

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.

Key Takeaways

Subtract datetime columns directly to get the difference as a Timedelta.
Use the .dt accessor to convert Timedelta to days, seconds, or other units.
pd.to_timedelta creates fixed durations for adding or subtracting time.
Choose subtraction for comparing dates, pd.to_timedelta for fixed intervals.
Both methods are simple and efficient for date difference tasks in pandas.