首页 > 解决方案 > How to create a pandas.date_range with a frequency of one hour excluding weekends?

问题描述

How to create a pandas.date_range with a frequency of one hour excluding weekends? Weekmask doesn't work with standart frequency '1H', or with ps.tseries.offsets.DateOffset(hours=1). And ps.offsets.BusinessHour(start='0:00', end='23:00') doesn't include 23:00. I'm out of ideas. Please, help. Thanks.

标签: pythonpandastime-seriesoffset

解决方案


I believe you need:

r = pd.date_range('2019-09-12', '2019-09-16', freq='H')
r = r[r.dayofweek < 5]
print (r)
DatetimeIndex(['2019-09-12 00:00:00', '2019-09-12 01:00:00',
               '2019-09-12 02:00:00', '2019-09-12 03:00:00',
               '2019-09-12 04:00:00', '2019-09-12 05:00:00',
               '2019-09-12 06:00:00', '2019-09-12 07:00:00',
               '2019-09-12 08:00:00', '2019-09-12 09:00:00',
               '2019-09-12 10:00:00', '2019-09-12 11:00:00',
               '2019-09-12 12:00:00', '2019-09-12 13:00:00',
               '2019-09-12 14:00:00', '2019-09-12 15:00:00',
               '2019-09-12 16:00:00', '2019-09-12 17:00:00',
               '2019-09-12 18:00:00', '2019-09-12 19:00:00',
               '2019-09-12 20:00:00', '2019-09-12 21:00:00',
               '2019-09-12 22:00:00', '2019-09-12 23:00:00',
               '2019-09-13 00:00:00', '2019-09-13 01:00:00',
               '2019-09-13 02:00:00', '2019-09-13 03:00:00',
               '2019-09-13 04:00:00', '2019-09-13 05:00:00',
               '2019-09-13 06:00:00', '2019-09-13 07:00:00',
               '2019-09-13 08:00:00', '2019-09-13 09:00:00',
               '2019-09-13 10:00:00', '2019-09-13 11:00:00',
               '2019-09-13 12:00:00', '2019-09-13 13:00:00',
               '2019-09-13 14:00:00', '2019-09-13 15:00:00',
               '2019-09-13 16:00:00', '2019-09-13 17:00:00',
               '2019-09-13 18:00:00', '2019-09-13 19:00:00',
               '2019-09-13 20:00:00', '2019-09-13 21:00:00',
               '2019-09-13 22:00:00', '2019-09-13 23:00:00',
               '2019-09-16 00:00:00'],
              dtype='datetime64[ns]', freq=None)

推荐阅读