使用 to_datetime
函数将列转换为日期时间格式,或使用to_timedelta
函数将其转换为时间差值格式,使用sub
函数进行减法运算,再使用total_seconds
将输出的时间差值转换为总秒数并除以60
:
start_time = pd.to_datetime(df['start_time'].astype(str))
end_time = pd.to_datetime(df['end_time'].astype(str))
#another solution
#start_time = pd.to_timedelta(df['start_time'].astype(str))
#end_time = pd.to_timedelta(df['end_time'].astype(str))
df['time_diff'] = start_time.sub(end_time).dt.total_seconds().div(60)
print (df)
code start_time end_time time_diff
0 0 13:00:09 13:30:09 -30.000000
1 1 14:23:33 15:23:23 -59.833333
2 2 11:30:00 12:30:00 -60.000000
如果将结束时间与开始时间交换:
df['time_diff'] = end_time.sub(start_time).dt.total_seconds().div(60)
print (df)
code start_time end_time time_diff
0 0 13:00:09 13:30:09 30.000000
1 1 14:23:33 15:23:23 59.833333
2 2 11:30:00 12:30:00 60.000000
示例:
import datetime
df = pd.DataFrame({'code': [0, 1, 2],
'start_time': [datetime.time(13, 0, 9), datetime.time(14, 23, 33),
datetime.time(11, 30)],
'end_time': [datetime.time(13, 30, 9), datetime.time(15, 23, 23),
datetime.time(12, 30)]})
print (df)
code start_time end_time
0 0 13:00:09 13:30:09
1 1 14:23:33 15:23:23
2 2 11:30:00 12:30:00
感谢Anton vBR提供的评论中的IDE:
start_time = pd.to_timedelta(df['start_time'].astype(str)).dt.total_seconds()
end_time = pd.to_timedelta(df['end_time'].astype(str)).dt.total_seconds()
df['time_diff'] = end_time.sub(start_time).div(60)
print (df)
code start_time end_time time_diff
0 0 13:00:09 13:30:09 30.000000
1 1 14:23:33 15:23:23 59.833333
2 2 11:30:00 12:30:00 60.000000