The Python Oracle

Pandas between_time boolean

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Pandas Between_time Boolean
00:56 Accepted Answer Score 6
01:29 Thank you

--

Full question
https://stackoverflow.com/questions/3500...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 6


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