Home > database >  How to compare datetime.time objects
How to compare datetime.time objects

Time:11-21

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 ?!

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

Datetime.time object represents time independent from the day. E.g.

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 e.g.

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, e.g.

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