### 如何以分钟为单位获取时差

``````  code     start_time      end_time
0        13:00:09        13:30:09
1        14:23:33        15:23:23
2        11:30:00        12:30:00
``````

`start_time``end_time`属于对象类型。

``````df['time_diff'] = pd.Timestamp(df['start_time']) - pd.Timestamp(df['end_time'])
``````

#### 1 个答案:

``````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
``````

IDE已发表评论，感谢@Anton vBR：

``````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
``````