What I am trying to do is if 07:30:00 between 20:00:00 = daylight
And this doesn't work because of the leading zeros:
df.loc[df['Time_of_Day'] <= 07:30:00 and df['Time_of_Day'] <= 20:00:00]='daylight'
Dataframe
Time_of_Day
13:30:00
11:30:00
00:01:00
etc.
CodePudding user response:
you need to use parenthesis and quotes like this :
df.loc[(df['Time_of_Day'] >= '07:30:00') & (df['Time_of_Day'] <= '20:00:00'), 'Time_of_Day'] = 'daylight'
Date Description Time_of_Day
0 09/05/2015 01:30:00 PM DOMESTIC BATTERY SIMPLE daylight
1 09/04/2015 11:30:00 AM POCKET-PICKING daylight
2 09/01/2018 12:01:00 AM OVER $500 00:01:00
3 09/05/2015 12:45:00 PM POSS: HEROIN(BRN/TAN) daylight
4 09/05/2015 01:00:00 PM SIMPLE daylight
(and i guess >= 7:30 instead of <=)
CodePudding user response:
# create a time field from datetime
df['time']=pd.to_datetime(df['Date']).dt.strftime('%H:%M:%S')
# use LOC to identify the rows matching criteria then set a field to 'daylight'
# i created a column named daylight, which could be anything
df.loc[(df['time']>='07:00:00') &
(df['time']<='20:00:00'), 'daylight'] = 'daylight'
df
Date Description Time_of_Day time daylight
0 09/05/2015 01:30:00 PM DOMESTIC BATTERY SIMPLE 13:30:00 13:30:00 daylight
1 09/04/2015 11:30:00 AM POCKET-PICKING 11:30:00 11:30:00 daylight
2 09/01/2018 12:01:00 AM OVER $500 00:01:00 00:01:00 NaN
3 09/05/2015 12:45:00 PM POSS: HEROIN(BRN/TAN) 12:45:00 12:45:00 daylight
4 09/05/2015 01:00:00 PM SIMPLE 13:00:0 13:00:00 daylight