Pravat
Pravat

Reputation: 329

Calculate the Time Difference in Second

I have a Dataframe where last two columns refers to Clock time. DataType of both columns are String.

Sample data looks like:

PROCESS_N   VAL     DATE        TIME_1      TIME_2
35324399    74.95   02/11/18    12:45:26    12:30:36
35324399    74.95   02/11/18    12:45:26    12:35:36
35334154    74.95   02/11/18    12:45:27    12:36:22
35338698    74.95   02/11/18    12:45:28    12:38:30
35338698    74.95   02/11/18    12:45:28    12:38:32
35347905    74.95   02/11/18    12:45:30    12:39:36
35367939    74.95   02/11/18    12:45:30    12:39:39
35371892    74.95   02/11/18    12:45:31    12:39:55

I want to populate another column in the end of Dataframe with difference of TIME_1 and TIME_2 in Second.

Upvotes: 0

Views: 141

Answers (5)

jpp
jpp

Reputation: 164673

You can use pd.to_timedelta and then access pd.Series.dt.seconds:

time_cols = ['TIME_1', 'TIME_2']
df[time_cols] = df[time_cols].apply(pd.to_timedelta)

df['Diff'] = (df['TIME_1'] - df['TIME_2']).dt.seconds

print(df)

   PROCESS_N    VAL      DATE   TIME_1   TIME_2  Diff
0   35324399  74.95  02/11/18 12:45:26 12:30:36   890
1   35324399  74.95  02/11/18 12:45:26 12:35:36   590
2   35334154  74.95  02/11/18 12:45:27 12:36:22   545
3   35338698  74.95  02/11/18 12:45:28 12:38:30   418
4   35338698  74.95  02/11/18 12:45:28 12:38:32   416
5   35347905  74.95  02/11/18 12:45:30 12:39:36   354
6   35367939  74.95  02/11/18 12:45:30 12:39:39   351
7   35371892  74.95  02/11/18 12:45:31 12:39:55   336

Upvotes: 2

Saeed Heidari
Saeed Heidari

Reputation: 419

if you know how to use data frames this code will help you in good way:

import datetime
import time

x1 = time.strptime('12:45:26,000'.split(',')[0], '%H:%M:%S')
x2 = time.strptime('12:30:36,000'.split(',')[0], '%H:%M:%S')
delta_x = datetime.timedelta(hours=x1.tm_hour - x2.tm_hour, minutes=x1.tm_min - 
x2.tm_min, seconds=x1.tm_sec - x2.tm_sec).total_seconds()
print(delta_x)

Upvotes: 1

today
today

Reputation: 33410

You can simply use to_datetime() method to convert and then take the difference:

df['diff'] = pd.to_datetime(df['TIME_1']) - pd.to_datetime(df['TIME_2'])

If you want it to be stored in seconds you can convert it like this:

df['diff'] = df['diff'].astype('timedelta64[s]')

The output:

  PROCESS_N    VAL      DATE    TIME_1    TIME_2   diff
0   35324399  74.95  02/11/18  12:45:26  12:30:36  890.0
1   35324399  74.95  02/11/18  12:45:26  12:35:36  590.0
2   35334154  74.95  02/11/18  12:45:27  12:36:22  545.0
3   35338698  74.95  02/11/18  12:45:28  12:38:30  418.0
4   35338698  74.95  02/11/18  12:45:28  12:38:32  416.0
5   35347905  74.95  02/11/18  12:45:30  12:39:36  354.0
6   35367939  74.95  02/11/18  12:45:30  12:39:39  351.0
7   35371892  74.95  02/11/18  12:45:31  12:39:55  336.0

Upvotes: 1

luke
luke

Reputation: 11

Take a look at convering both your numbers to datetime objects and reduce one from another, like this:

import datetime as dt

time1 = dt.datetime(2018,12,30,23,59,59)
time2 = dt.datetime(2018,12,31,23,59,59)

(time2-time1).total_seconds()

(source)

Upvotes: 0

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

Difference in secs using timedelta -

df['Diff'] = (df['TIME_1']-df['TIME_2']).astype('timedelta64[s]')

Output

   PROCESS_N    VAL      DATE              TIME_1              TIME_2   Diff
0   35324399  74.95  02/11/18 1900-01-01 12:45:26 1900-01-01 12:30:36  890.0
1   35324399  74.95  02/11/18 1900-01-01 12:45:26 1900-01-01 12:35:36  590.0
2   35334154  74.95  02/11/18 1900-01-01 12:45:27 1900-01-01 12:36:22  545.0
3   35338698  74.95  02/11/18 1900-01-01 12:45:28 1900-01-01 12:38:30  418.0
4   35338698  74.95  02/11/18 1900-01-01 12:45:28 1900-01-01 12:38:32  416.0
5   35347905  74.95  02/11/18 1900-01-01 12:45:30 1900-01-01 12:39:36  354.0
6   35367939  74.95  02/11/18 1900-01-01 12:45:30 1900-01-01 12:39:39  351.0
7   35371892  74.95  02/11/18 1900-01-01 12:45:31 1900-01-01 12:39:55  336.0

Cast to date before calculating the difference in case you have str -

df['TIME_1'] = pd.to_datetime(df['TIME_1'],format='%H:%M:%S')
df['TIME_2'] = pd.to_datetime(df['TIME_2'],format='%H:%M:%S')

Upvotes: 1

Related Questions