Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas date_range with specific time ranges

I am trying to use pd.date_range() to create a DatetimeIndex that spans over multiple days with a frequency in seconds. However, the time range for each day should be restricted between 05:00:00 and 22:00:00

Something along the lines of (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]
like image 821
user3142067 Avatar asked Dec 23 '22 17:12

user3142067


1 Answers

Use indexer_between_time:

times_c[times_c.indexer_between_time('05:00:00', '22:00:00')]
like image 196
root Avatar answered Jan 01 '23 03:01

root