I have a large excel file with start and finish times for marathon runners. In order to determine the number of runners still on the course after delayed start times, I've tried to import the data into Pandas and use the built in pandas comparison in order to return a list of runners running at a certain time. At a given time x, runners on the course would have a start time <= x and a endtime > x. However in Pandas one of these is giving me an error.
I've imported the dataframe from Excel using the read_exel which automatically converts the start times and end times as Datetime.time objects. Here's some sample data
df = pd.DataFrame(
{'name':['Bob','Sue','Joe'],
'start_time':[datetime.time(6,50,0),datetime.time(6,55,0),datetime.time(7,0,0)],
'start_time':[datetime.time(7,15,04),datetime.time(7,21,41),datetime.time(7,23,24)],})
Runners start at 6:50 and I would like to make a list of the amount of runners on the course every 4 minutes. So I've set up some variables to handle that:
race_start = datetime.datetime(100,1,1,6,50)
intervaul = datetime.timedelta(minutes = 4)
capture_time = race_start
Then I try to select the correct rows using Pandas built in selection
df[df.start_time <= capture_time.time() & df.end_time > capture_time.time()]
However I get the error:
TypeError: Cannot compare datetime.time and unicode
In fact, df.start_time <= capture_time.time() is perfectly fine and runs, but df.end_time <= capture_time.time() returns this error.
I have no idea what is going on here and any help would be appreciated.
You need add () twice only, first can be omit, but by best practices are used too:
pd[(pd.start_time <= capture_time.time()) & (pd.end_time <= capture_time.time())]
Or maybe dtype of column end_time is not datetime, so you can use to_datetime:
pd.end_time = pandas.to_datetime(pd.end_time)
I think name for DataFrame is better df, then you can use:
import pandas as pd
df.end_time = pd.to_datetime(df.end_time)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With