我的主要目的是找出每个工作所花的总时间。我确实尝试减去两个时间列,但出现了一个错误:<class 'datetime.time'> is not convertible to datetime
。你知道吗
我运行了.info()并看到时间列是对象。在excel文件中,格式仅为时间,而不是日期时间格式。我尝试将第一时间列转换为日期时间格式,结果如下:
import pandas as pd
df = pd.read_excel('C:/users/paul/desktop/data project/July.xls', index_col=0)
hrs_st = (pd.to_datetime(df['AST'].str.strip(), format='%H:%M:%S'))
print (hrs_st)
工作指令
BAEBRO-906063 NaT
BAEBRO-906191 NaT
BAEBRO-906207 NaT
BAEBRO-906079 NaT
BAEBRO-906095 NaT
BAEBRO-906159 NaT
...
说实话有点混乱。你能不能更具体一点,你的主要目标是什么,并提供更多的资料,日期是如何显示在你的excel文件。你知道吗
第二次编辑* |我试着用代码注释我写的东西。你知道吗
我举了一个类似的例子,只是想知道我能帮你什么。你知道吗
我的excel文件如下所示:
这就是以非常简单的方式读取和计算差异的代码:
这是我在输出中得到的:
我想出了一个更好的解决方案,对我原来的问题非常有效,那就是计算出完成一个工作单所花费的总时间。这个解决方案有助于克服excel时间格式这是一个对象类型。一旦转换为datetime[64]启用,一列减去另一列。你知道吗
import pandas as pd from datetime import time from datetime import timedelta
df = pd.read_excel('C:/Users/Nativ_Zero/Desktop/work data/July.xls', index_col =0)
df_work = df[['WorkType', 'AST','AFT']]
#to convert time format column which is an object to datetime[64 df_work['AFT'] = pd.to_datetime(df_work['AFT'], format='%H:%M:%S', errors='coerce') df_work['AST'] = pd.to_datetime(df_work['AST'], format='%H:%M:%S', errors='coerce')
rm_work = df_work[df_work.WorkType == 'RM']
hrs_ft = rm_work['AFT'] hrs_st = rm_work['AST']
hrs_t = hrs_ft - hrs_st
相关问题 更多 >
编程相关推荐