'How do I subtract two datetime in Pandas?

I have one column

  1. ivr_table_time['payment_time'] giving me value like : 2021-11-06 14:00:56.826503
  2. ivr_table_time['call_time'] giving me value like: 2021-11-04 18:30:20

I want to find if payment was done within 15 mins of the call. I used the following condition - np.where((ivr_table_time['payment_time'] - ivr_table_time['call_time'])<='00:15:00', 1, 0) but I am not getting the correct answer. How do I approach this? Please help.



Solution 1:[1]

IIUC use:

np.where((ivr_table_time['payment_time'] - ivr_table_time['call_time'])<= pd.Timedelta('00:15:00'), 1, 0) 

Or:

(ivr_table_time['payment_time'] - ivr_table_time['call_time']<= pd.Timedelta('00:15:00')).astype(int)

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 jezrael