I have a column in pandas df with string datetime like below
a,dtime
1,2017-07-06 09:15:00
1,2017-07-06 10:15:00
I am writing a script that needs to compare time
I need to compare like df[df.dtime < "10:15:00"]
(without date)
So I converted df['dtime']=pd.to_datetime(df['dtime'])
If I do
df[df.dtime < "10:15:00"]
it takes today date as default and would always compare with today's "10:15:00" what I don't want.
So I created another column and then did it like below
df['ts']=df.dtime.apply(lambda x: x.time())
df[df.ts<"09:16:00"]
TypeError: can't compare datetime.time to str
df[df.ts<pd.to_datetime("09:16:00").time()] #this works well
Is there a better way to do this simple task, I dont see any point creating a new ts column.
When I do
df['dtime']=pd.to_datetime(df['dtime'])
I should only extract time part. But doing df['dtime']=pd.to_datetime(df['dtime']).time()
gives error AttributeError: 'Series' object has no attribute 'time'
You need to use time
or timedelta
instead of datetime
. You can access it vie the .dt.
methods
t = pd.to_datetime('10:15:00').time()
df['dtime'].dt.time < t
0 True
1 False
Name: dtime, dtype: bool