Pete
Pete

Reputation: 449

How do I compare a time value to all the time values in a column in pandas?

I want to apply a condition to a column in a dataframe. The column has time values in the format HH:MM:SS and I want to compare it to a fixed time value(threshold), but I am getting errors. Converting the threshold value to datetime.datetime.strptime gives me error. What I want to ask is - In order to compare any two time values how do I store them or what do I convert them into so that they can be compared? The threshold value is in string and the column values are in datetime.time format.

When I do this-

threshold = datetime.datetime.strptime('1:04:00','%H:%M:%S').time()  
df_styled = df.style.apply(lambda x:['background:red' if x > threshold else "background:white" for x in df['Total Time']], axis=0) 

I get -

TypeError: '>' not supported between instances of 'datetime.datetime' and 'datetime.time'

When I do this-

threshold = datetime.datetime.strptime('1:04:00','%H:%M:%S')
df['Total Time'] = df['Total Time'].apply(lambda x: datetime.datetime.strptime(x,'%H:%M:%S'))
df_styled = df.style.apply(lambda x:['background:red' if x > threshold else 'background:white' for x in df['Total Time']], axis=0)

I get the error

TypeError: strptime() argument 1 must be str, not datetime.time

Upvotes: 1

Views: 416

Answers (1)

jezrael
jezrael

Reputation: 862406

You can compare timedeltas created by Timedelta and to_timedelta:

threshold = pd.Timedelta('1:04:00')
df['Total Time'] = pd.to_timedelta(df['Total Time'].astype(str))

df_styled = df.style.apply(lambda x:['background:red' if x > threshold else "background:white" for x in df['Total Time']], axis=0) 

EDIT: Solution for your problematic data:

df['Total Time'] = pd.to_timedelta(pd.to_datetime(df['Total Time'].astype(str)).dt.strftime('%H:%M:%S'))

Upvotes: 2

Related Questions