Reputation: 1527
I am trying to create a new column to determine if a row value is "between business hours". To do this I am trying to use the between time function. I dont need to use it if there is an easier way.
I have a dataframe with columns for 'Date', 'StartHour', 'End Hour'.
Question:
I would like to give a 'True' or 'False' if the time in the 'Date' column is between the 'StartHour' and 'EndHour' time.
import pandas as pd
import numpy as np
#create dataframe with dates
d = {'Date': ['2016-11-17 05:01:45','2011-01-04 16:34:00','2011-01-05 09:25:45',
'2011-01-10 12:00:45','2011-01-14 07:05:45','2011-01-15 10:19:00',
'2011-01-17 13:59:45','2011-01-19 18:39:45','2011-01-22 06:19:45'],
'StartHour': ['16:00','16:00','16:00','16:00','16:00','16:00','16:00','16:00','16:00'],
'EndHour': ['10:00','10:00','10:00','10:00','10:00','10:00','10:00','10:00','10:00'],
'Station_ID': ['A','A','A','A','B','B','B','B','B']}
df = pd.DataFrame(data=d)
#convert date column to datetime
df['Date'] = df['Date'].values.astype('datetime64[ns]')
#************************
# set index to Date (need for 'between_time')
df = df.set_index('Date')
# run calculation for between time
df['between_business_hours'] = df.index.isin(df.between_time('16:00', '10:00', include_start=True, include_end=True).index)
df
I have calculated a column using the between_time function but this only lets me use hard coded values for the start and end time. I would like to use the values in the 'StartTime' and 'EndTime' columns. I am probably making this more difficult than it needs to be by using the between_time function.
I would like the output to looks something like this.
EndHour StartHour Station_ID between_business_hours
Date
2016-11-17 05:01:45 10:00 16:00 A True
2011-01-04 16:34:00 10:00 16:00 A True
2011-01-05 09:25:45 10:00 16:00 A True
2011-01-10 12:00:45 10:00 16:00 A False
2011-01-14 07:05:45 10:00 16:00 B True
2011-01-15 10:19:00 10:00 16:00 B False
2011-01-17 13:59:45 10:00 16:00 B False
2011-01-19 18:39:45 10:00 16:00 B True
2011-01-22 06:19:45 10:00 16:00 B True
Any help is appreciated
Upvotes: 0
Views: 1966
Reputation: 323266
You do not need set the index
df.Date.dt.strftime('%H:%M').between(df.StartHour,df.EndHour)
Out[297]:
0 False
1 True
2 True
3 True
4 False
5 True
6 True
7 True
8 False
dtype: bool
Update
l=[df.loc[[y],:].index.indexer_between_time(df.loc[y,'StartHour'],df.loc[y,'EndHour'])==0 for y in df.index]
df['New']=l
df.New=df.New.str[0].fillna(False)
df
EndHour StartHour Station_ID New
Date
2016-11-17 05:01:45 10:00 16:00 A True
2011-01-04 16:34:00 10:00 16:00 A True
2011-01-05 09:25:45 10:00 16:00 A True
2011-01-10 12:00:45 10:00 16:00 A False
2011-01-14 07:05:45 10:00 16:00 B True
2011-01-15 10:19:00 10:00 16:00 B False
2011-01-17 13:59:45 10:00 16:00 B False
2011-01-19 18:39:45 10:00 16:00 B True
2011-01-22 06:19:45 10:00 16:00 B True
Upvotes: 3