I'm trying to create a column that will assign true if row value falls between the times 09:00 and 17:00.
I can select those times easily enough using between_time but can't assign a new column a True, False.
df = df.between_time('9:00', '17:00', include_start=True, include_end=True)
This statement selects the appropriate rows, but I can't assign a True-False value to them.
I've been trying to use np.where.
df['day'] = np.where(df.between_time('9:00', '17:00', include_start=True, include_end=True),'True','False')
Except that np.where only works on boolean expressions.
I've used it like this:
df['day'] = np.where((df['dayTime'] < '09:00') & (df['dayTime'] > '17:00'),'True','False')
But only gotten False returns, because a value can't be greater than 17 and less than 9 at the same time.
I'm missing a relatively easy step and can't figure it out. Help.
You could do something like the following:
df.index.isin(df.between_time('9:00', '17:00', include_start=True, include_end=True).index)
Because the between method returns a dataframe, rather than a boolean array we can call .index on it as we could with any dataframe, this will return a series of times. We can then check wether or not our original time indices are in our time indices resulting from the between method.
This will give you a boolean array of values that are within your time frame. I can't think of another way to do this, but that doesn't mean there isn't one
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