Python Forum
How to calculate time difference between each row of dataframe in seconds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate time difference between each row of dataframe in seconds
#1
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')
Reply
#2
see: https://docs.python.org/3/library/time.h...ocess_time
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  time difference bettwenn logs enkliy 14 963 Nov-21-2023, 04:51 PM
Last Post: rob101
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Sum up Time difference tester_V 10 2,514 Apr-06-2023, 06:54 AM
Last Post: Gribouillis
  Using log to calculate difference EmBeck87 1 721 Apr-04-2023, 08:43 PM
Last Post: deanhystad
  Difference one column in a dataframe Scott 0 637 Feb-10-2023, 08:41 AM
Last Post: Scott
  Problem with module time and leap seconds Pedroski55 3 1,237 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,596 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Store variable data and display sum after 60 seconds the_dude 11 3,443 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  How to get indices of minimum time difference Mekala 1 2,147 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  Writing a function to calculate time range in Unix Epoch Time t4keheart 2 2,986 Jul-15-2020, 01:55 AM
Last Post: t4keheart

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020