import pandas as pd
df=pd.read_excel('/content/Haoling peak time data (1).xlsx')
df['Difference'] = df['ORDER END TIME']-df['ORDER START TIME']
错误:
TypeError:不支持的操作数类型-:'datetime '。Time '和'datetime.time'
时间必须转换为日期时间才能工作。你可以这样做。
from datetime import datetime, timedelta, date
def daytime(time):
return datetime.combine(date.min, time)
df['Difference'] = df['ORDER END TIME'].apply(daytime) - df['ORDER START TIME'].apply(daytime)
ORDER START TIME ORDER END TIME Difference
0 09:00:00 09:01:00 0 days 00:01:00
1 10:00:00 09:59:00 -1 days +23:59:00
假设一切都在24小时内,如果开始时间在结束时间之后,我们将有负的差异。如果我们需要解决这个问题,我们可以使用模。
day = timedelta(days=1)
df['Difference'] = df['Difference'] % day
ORDER START TIME ORDER END TIME Difference
0 09:00:00 09:01:00 0 days 00:01:00
1 10:00:00 09:59:00 0 days 23:59:00