Answer the question
In order to leave comments, you need to log in
How to convert type to datetime?
I welcome everyone! I have several columns in dataframe with data type object. Tell me how to reformat these columns in the hh:mm:ss time format so that they can be added?
For example, I have 2 columns (Time 1 and Time 2) with object data type, I want to get the sum of the two previous ones in the third column (Amount).
Time 1 Time 2 Total
0:41:50 0:00:00 0:41:50
1:51:01 0:27:50 2:18:51
2:17:57 0:21:42 2:39:39
2:14:21 0:17:14 2:31:35
Answer the question
In order to leave comments, you need to log in
Alternatively, if the difference is within a day:
import datetime
tm1 = '1:51:01'
tm2 = '0:27:50'
to_seconds = lambda x: int(x.split(':')[2]) + int(x.split(':')[1]) * 60 + int(x.split(':')[0])* 60 * 60
date1 = datetime.datetime.strptime(tm1, '%H:%M:%S')
date2 = date1 + datetime.timedelta(seconds=to_seconds(tm2))
print(date2.strftime('%H:%M:%S'))
# 02:18:51
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question