简体   繁体   中英

How to compare datetime.time objects

I have a column in my Dataframe that contains datetime.time() values. example:

--> df.loc[0,'tat']
output: datetime.time(0, 21, 4)

I want to write multiple if conditions with this column. example:

--> if df.loc[0,'tat'] < 2:
     df.loc[0,'SLA'] = 'less than 2 hour SLA'
    else:
     df.loc[0,'SLA'] = 'greater than 2 hour SLA'

--> if df.loc[0,'tat'] < 4 and df.loc[0,'tat'] > 2:
     df.loc[0,'SLA'] = '2-4 hour SLA'
    else:
     df.loc[0,'SLA'] = 'greater than 4 hour SLA'

When I compare df.loc[r,'tat']< 2 it gives a TypeError: '<' not supported between instances of 'datetime.time' and 'int'

I then tried to create timedeltas.

timedelta_2 = timedelta(hours=2)
df.loc[r,'tat']< timedelta_2

It still gives me a TypeError: '<' not supported between instances of 'datetime.time' and 'datetime.timedelta'

How else am I supposed to compare?!

You need compare hours with scalars, solution for new helper column hour with cut :

hours = pd.to_datetime(df['tat'].astype(str)).dt.hour

hours = df['tat'].apply(lambda x: x.hour)

df['SLA'] = pd.cut(hours, bins=[0,2,3,24], 
                  labels=['less than 2 hour SLA','2-4 hour SLA','greater than 4 hour SLA'])

Or you can extract hour from datetime.time objects:

if df.loc[0,'tat'].hour < 2:
     df.loc[0,'SLA'] = 'less than 2 hour SLA'
elif (df.loc[0,'tat'].hour < 4) and (df.loc[0,'tat'].hour > 2):
     df.loc[0,'SLA'] = '2-4 hour SLA'
else:
     df.loc[0,'SLA'] = 'greater than 4 hour SLA'

Solution for new column SLA :

def func(x):

    if x.hour < 2:
         return 'less than 2 hour SLA'
    elif (x.hour < 4) and (x.hour > 2):
        return '2-4 hour SLA'
    else:
         return 'greater than 4 hour SLA'

df['SLA'] = df['tat'].apply(func)

You cannot compare a dattime.time instance with in integer
what you are doing is compaing apples with oranages
i would suggest converting the int to a time instance

import datetime.datetime as dt
if df.loc[0,'tat'] < dt.time(hours=2,minutes=0,seconds=0):
    df.loc[0,'SLA'] = 'less than 2 hour SLA'
else:
    df.loc[0,'SLA'] = 'greater than 2 hour SLA'

if df.loc[0,'tat'] < dt.time(4,0,0) and df.loc[0,'tat'] > dt.time(hours=2,minutes=0,seconds=0):
    df.loc[0,'SLA'] = '2-4 hour SLA'
else:
    df.loc[0,'SLA'] = 'greater than 4 hour SLA'

I would also suggest that you make sure both the columns ('tat', 'SLA') are of datetime.time instance

Datetime.time object represents time independent from the day. Eg

datetime.time(0, 21, 4)

Translates to 00:21:04 AM

Source: https://docs.python.org/3/library/datetime.html#date-objects

So what you probably need is something to compare the 'tat' time to, in other words another datetime.time object eg

if df.loc[0,'tat']-other_datetime_time_object < timedelta(hours=2):

Or if 'tat' is actually representing duration of something, it should be timedelta object instead.

EDIT: If you cannot change the object type, the dirtiest hack would be to compare it to 00:00:00 hrs, eg

if df.loc[0,'tat']-datetime.time(0,0,0) < timedelta(hours=2):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM