Reputation: 1784
I m trying to create a bool with multiple conditions on a datetimeindex. Here is my example:
df = pd.DataFrame(index=pd.date_range('2020-05-24', '2020-05-26', freq='1H', closed='left'))
mybool = np.logical_and(df.index.weekday < 5 , df.index.hour > 7 , df.index.hour < 20)
So mybool should be True for Mon-Friday for 12 hrs from 8am to 8pm. However this returns true on Mon-Fri from 8am to midnight. So it looks like the first two conditions are picked up but the third is not. But this also returns no error.
Upvotes: 2
Views: 48
Reputation: 88236
Chain your conditions with bitwise ands:
(df.index.weekday < 5) & (df.index.hour > 7) & (df.index.hour < 20)
Note that np.logical_and
expects only two input arrays x1, x2
. An alternative would be to use np.logical_and.reduce
on a list of conditions:
np.logical_and.reduce([df.index.weekday < 5, df.index.hour > 7, df.index.hour < 20])
Upvotes: 1