Search code examples
python-3.xcomparisonwhere-clause

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


I need to check if an employee has checked out during the break. To do so, I need to see if there is the time in which Door Name is RDC_OUT-1 is in the interval [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')

When running the code above, I got this error:

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'


Solution

  • When you are iterating the DataFrame/Series you are selecting one cell at a time.

    The cell which you are Selecting is of type datetime.time

    However, where only works with the complete DataFrame/Series rather than having this in a loop.

    Like,

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

    and then to count you can use len(sub_df)