2

I’m working on a project and I need to subtract two date time values to get the number of days. A sample of the data can be seen below:


                   ALARM_DATE               CONT_DATE  \
0      2020/06/18 00:00:00+00  2020/06/23 00:00:00+00   
1      2020/06/01 00:00:00+00  2020/06/04 00:00:00+00   
2      2020/08/10 00:00:00+00  2020/03/01 00:00:00+00   
3      2020/03/31 00:00:00+00  2020/04/01 00:00:00+00   
4      2020/04/14 00:00:00+00  2020/04/19 00:00:00+00   
...                       ...                     ...   

I tried simply subtracting the values, but obviously that didn’t work. Can anyone please help?

2

1 Answer 1

2

Convert your columns to actual dates first:

df['ALARM_DATE'] = pd.to_datetime(df['ALARM_DATE'])
df['CONT_DATE'] = pd.to_datetime(df['CONT_DATE'])

Or:

df[['ALARM_DATE', 'CONT_DATE']] = df[['ALARM_DATE', 'CONT_DATE']].apply(pd.to_datetime)

Output:

>>> df['CONT_DATE'] - df['ALARM_DATE']
0      5 days
1      3 days
2   -162 days
3      1 days
4      5 days
dtype: timedelta64[ns]
2
  • 1
    Looks like I'm getting an error here: TypeError: Unrecognized value type: <class 'str'>
    – Aarian
    Mar 24, 2022 at 2:17
  • 1
    Update: your code does work! The issue was that I had to remove null values using dropna()
    – Aarian
    Mar 24, 2022 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.