How can I speed up the Pandas.index apply?

Viewed 29

I want to create vector/array of when DataFrame index, which is a DateTimeIndex, is between hours of 8 and 14.

This works but is very slow:

permitted = flose.index.to_series().apply(lambda x: 1 if 8 <= x.hour <= 14 else 0)

I am tried variations of this but always get errors:

permitted = np.where(8 <= flose.index.values.hour <= 14 ,1,0)
1 Answers

Try:

permitted = ((8 <= flose.index.to_series().dt.hour) & (flose.index.to_series().dt.hour <= 14)).astype(int)
Related