'Subtract two dates in python when "Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported."
I'm trying to calculate the difference between two dates in python. You used to be able to do that very simply, by just doing df['date1'] - df['date2] (assuming your dates are in datetime). But now when I do that, I get the exception:
TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting `n`, use `n * obj.freq`
The only answered question to this issue is for someone adding a known amount of days to a date. I do not know what the amount of days between these two dates is, hence why I'm trying to calculate it. I don't understand how 'use n * obj.freq' is supposed to help me with this use case.
id. date1. date2
1. 2022-01-01. 2022-02-01
2. 2021-10-15. 2022-10-17
3. 2022-01-24. 2022-03-24
I want to add a column that calculates the difference between these dates and expresses it in days, e.g.,
id. date1. date2. delta
1. 2022-01-01. 2022-02-01. 31
2. 2021-10-15. 2022-10-17. 2
3. 2022-01-24. 2022-03-24. 59
Solution 1:[1]
Subtraction should work directly. However, you need n * obj.freq for the modulo:
from pandas.tseries.offsets import Day
df['date1'] = pd.to_datetime(df['date1'])
df['date2'] = pd.to_datetime(df['date2'])
df['delta'] = df['date2'].sub(df['date1']).mod(365*Day()).dt.days
Output:
id date1 date2 delta
0 1 2022-01-01 2022-02-01 31
1 2 2021-10-15 2022-10-17 2
2 3 2022-01-24 2022-03-24 59
Used input:
df = pd.DataFrame({'id': [1, 2, 3],
'date1': [Timestamp('2022-01-01 00:00:00'),
Timestamp('2021-10-15 00:00:00'),
Timestamp('2022-01-24 00:00:00')],
'date2': [Timestamp('2022-02-01 00:00:00'),
Timestamp('2022-10-17 00:00:00'),
Timestamp('2022-03-24 00:00:00')]})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
