Skip to content
Advertisement

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-

JavaScript

I get –

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

When I do this-

JavaScript

I get the error

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

Advertisement

Answer

You can compare timedeltas created by Timedelta and to_timedelta:

JavaScript

EDIT: Solution for your problematic data:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement