Python Forum
How to calculate time difference between each row of dataframe in seconds - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to calculate time difference between each row of dataframe in seconds (/thread-28374.html)



How to calculate time difference between each row of dataframe in seconds - Mekala - Jul-16-2020

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')



RE: How to calculate time difference between each row of dataframe in seconds - Larz60+ - Jul-16-2020

see: https://docs.python.org/3/library/time.html#time.process_time