Python Forum

Full Version: How to calculate time difference between each row of dataframe in seconds
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have below data and want to calculate time difference,
I use below code: it give the below error:

TypeError: ufunc true_divide cannot use operands with types dtype('float64') and dtype('<m8[m]')

which way we can calculate time delta in seconds.


my input file:


Date_time	            event_ID	Name	Rank
2020-07-13 20:13:25	    E1	        KMM	    2
2020-07-13 20:14:35	    CI_345	    DLH	    8
2020-07-17 13:14:56	    HJ_9078	    NYJ	    7
2020-07-18 13:14:56	    EY_20	    GNT	    12
import pandas as pd
import datetime
import numpy as np
df=pd.read_excel(r'D:\PythonCodes\input_time.xlsx')
df['tvalue'] = df.index
df['delta'] = (df['tvalue']-df['tvalue'].shift()).fillna(0)
import pandas as pd
import datetime
import numpy as np
df=pd.read_excel(r'D:\Mekala_Backupdata\PythonCodes\input_time.xlsx')
df['tvalue'] = df.index
df['delta'] = (df['tvalue']-df['tvalue'].shift()).fillna(0)
df['ans'] = df['delta'].apply(lambda x: x  / np.timedelta64(1,'m')).astype('int64')