Skip to content Skip to sidebar Skip to footer

How To Add Integer (seconds) To Hh:mm:ss Format In Python?

I have the following data frame in python where am trying to compute column 'New time' by adding 'Duration' (which is in seconds) to the 'start_time' Serial start_date start_t

Solution 1:

You can use to_timedelta and output is timedelta too:

df['New time'] = pd.to_timedelta(df['start_time']) + 
                 pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
  Serial start_date start_time  Duration(seconds) New time
0      A  5/22/2017   10:37:24                216 10:41:00
1      A  5/22/2017   10:37:26                213 10:40:59
2      A  5/22/2017   10:37:29                  3 10:37:32
3      A  5/22/2017   10:39:55                 60 10:40:55
4      A  5/22/2017   10:51:50                380 10:58:10
5      A  5/22/2017   10:51:57                339 10:57:36

But if seconds is more, output is changed, because there are also days:

print (df)
  Serial start_date start_time  Duration(seconds)
0      A  5/22/2017   10:37:24                216
1      A  5/22/2017   10:37:26             213000
2      A  5/22/2017   10:37:29                  3
3      A  5/22/2017   10:39:55                 60
4      A  5/22/2017   10:51:50                380
5      A  5/22/2017   10:51:57                339

df['New time'] = pd.to_timedelta(df['start_time']) + 
                 pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
  Serial start_date start_time  Duration(seconds)        New time
0      A  5/22/2017   10:37:24                216 0 days 10:41:00
1      A  5/22/2017   10:37:26             213000 2 days 21:47:26
2      A  5/22/2017   10:37:29                  3 0 days 10:37:32
3      A  5/22/2017   10:39:55                 60 0 days 10:40:55
4      A  5/22/2017   10:51:50                380 0 days 10:58:10
5      A  5/22/2017   10:51:57                339 0 days 10:57:36

Also is posible add datetime:

df['New date'] = pd.to_datetime(df['start_date']) + \
                 pd.to_timedelta(df['start_time']) +  \
                 pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
  Serial start_date start_time  Duration(seconds)            New date
0      A  5/22/2017   10:37:24                216 2017-05-22 10:41:00
1      A  5/22/2017   10:37:26                213 2017-05-22 10:40:59
2      A  5/22/2017   10:37:29                  3 2017-05-22 10:37:32
3      A  5/22/2017   10:39:55                 60 2017-05-22 10:40:55
4      A  5/22/2017   10:51:50                380 2017-05-22 10:58:10
5      A  5/22/2017   10:51:57                339 2017-05-22 10:57:36

df['New date'] = pd.to_datetime(df['start_date']) + \
                 pd.to_timedelta(df['start_time']) +  \
                 pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
  Serial start_date start_time  Duration(seconds)            New date
0      A  5/22/2017   10:37:24                216 2017-05-22 10:41:00
1      A  5/22/2017   10:37:26             213000 2017-05-24 21:47:26
2      A  5/22/2017   10:37:29                  3 2017-05-22 10:37:32
3      A  5/22/2017   10:39:55                 60 2017-05-22 10:40:55
4      A  5/22/2017   10:51:50                380 2017-05-22 10:58:10
5      A  5/22/2017   10:51:57                339 2017-05-22 10:57:36

---

And if need convert timedelta to string in format HH:MM:SS and lost days (if exist):

df['New time'] = pd.to_timedelta(df['start_time']) + 
                 pd.to_timedelta(df['Duration(seconds)'], unit='s')
df['New time'] = pd.to_datetime(df['New time']).dt.strftime('%H:%M:%S')
print (df)
  Serial start_date start_time  Duration(seconds)  New time
0      A  5/22/2017   10:37:24                216  10:41:00
1      A  5/22/2017   10:37:26             213000  21:47:26
2      A  5/22/2017   10:37:29                  3  10:37:32
3      A  5/22/2017   10:39:55                 60  10:40:55
4      A  5/22/2017   10:51:50                380  10:58:10
5      A  5/22/2017   10:51:57                339  10:57:36

Solution 2:

Here is a snippet that may help you solve the problem yourself:

from datetime import datetime, timedeltamy_date= datetime.strptime('5/22/2017 10:37:24', '%m/%d/%Y %H:%M:%S')
my_time_diff = timedelta(seconds=216)
my_new_date = my_date + my_time_diff
print(my_new_date.strftime('%m/%d/%Y %H:%M:%S'))

Useful resource:

Post a Comment for "How To Add Integer (seconds) To Hh:mm:ss Format In Python?"