'datetime.time' 对象没有属性 'where' Python

'datetime.time' object has no attribute 'where' Python

我需要检查员工是否在休息期间退房。 为此,我需要查看 time 其中 Door NameRDC_OUT-1 在区间 [12:15: 00; 14:15:00]

import pandas as pd

df_by_date= pd.DataFrame({'Time':['01/02/2019 07:02:07', '01/02/2019 10:16:55', '01/02/2019 12:27:20', '01/02/2019 14:08:58','01/02/2019 15:32:28','01/02/2019 17:38:54'],
 'Door Name':['RDC_OUT-1', 'RDC_IN-1','RDC_OUT-1','RDC_IN-1','RDC_OUT-1','RDC_IN-1']})



df_by_date['Time'] = pd.to_datetime(df_by_date['Time'])
df_by_date['hours']=pd.to_datetime(df_by_date['Time'], format='%H:%M:%S').apply(lambda x: x.time())
print('hours \n',df_by_date['hours'])

out = '12:15:00'
inn = '14:15:00'
pause=0
for i in range (len(df_by_date)):
    if (out < str((df_by_date['hours'].iloc[i]).where(df_by_date['Door Name'].iloc[i]=='RDC_IN-1')) < inn)  :
        pause+=1
        print('Break outside ')
    else:
        print('Break inside')

当运行上面的代码时,我得到了这个错误:

if (out < ((df_by_date['hours'].iloc[i]).where(df_by_date['Door Name'].iloc[i]=='RDC_OUT-1')) < inn)  :

AttributeError: 'datetime.time' object has no attribute 'where'

当您迭代 DataFrame/Series 时,您一次选择一个单元格。

您选择的单元格类型为datetime.time

但是,where 仅适用于完整的 DataFrame/Series,而不是循环使用。

喜欢,

sub_df = df_by_date['hours'].where(condition)

然后计算你可以使用 len(sub_df)