Pandas date_range with specific time ranges

I am trying to use pd.date_range()

to create DatetimeIndex

that spans several days with a frequency of seconds. However, the time range for each day must be limited between 05:00:00 and 22:00:00

Something like strings (doesn't work of course):

times_c = pd.date_range(start="2015-01-01",end="2015-01-10",freq="S")
mask_c = ((times_c.time < dt.datetime.strptime("22:00:00", "%H:%M:%S")) | (times_c.time > dt.datetime.strptime("05:00:00","%H:%M:%S")))
times_c = times_c[mask_c]

      

+3


source to share


3 answers


Use indexer_between_time

:



times_c[times_c.indexer_between_time('05:00:00', '22:00:00')]

      

+4


source


It works?



times_c = pd.date_range(start="2015-01-01", end="2015-01-10", freq="S")
times_c[(times_c.hour<22) | (times_c.hour>5)]

      

+1


source


Try it.

times_c[((times_c.hour >= 5) & (times_c.hour < 22)) | ((times_c.hour == 22) &(times_c.minute == 0) & (times_c.second == 0))]

      

Let's get all time periods greater than 5 hour and less than 22 and then get this edge case 22 with zero minutes and zero seconds.

+1


source







All Articles