python 函数 日期区间_python-如何获取两个日期之间的日期
這是一個使用“業務”日過濾和自定義周掩碼的numpy解決方案:
>>> all_days = np.arange('1970-02-05', '1970-05-08', dtype='M8[D]')
>>> week_start_end = all_days[np.is_busday(all_days, weekmask='Mon Sun')]
>>> week_start_end
array(['1970-02-08', '1970-02-09', '1970-02-15', '1970-02-16',
'1970-02-22', '1970-02-23', '1970-03-01', '1970-03-02',
'1970-03-08', '1970-03-09', '1970-03-15', '1970-03-16',
'1970-03-22', '1970-03-23', '1970-03-29', '1970-03-30',
'1970-04-05', '1970-04-06', '1970-04-12', '1970-04-13',
'1970-04-19', '1970-04-20', '1970-04-26', '1970-04-27',
'1970-05-03', '1970-05-04'], dtype='datetime64[D]')
這僅適用于數周.對于其他單位:
>>> def first_last_range(start, stop, step='M', resolution='D'):
... large = np.arange(start, stop, dtype=f'M8[{step}]')
... first = large.astype(f'M8[{resolution}]')
... last = (large + np.timedelta64(1, step)).astype(f'M8[{resolution}]') - np.timedelta64(1, resolution)
... full = np.c_[first, last].ravel()
... return full[full[0] < np.datetime64(start) : len(full) - (full[-1] > np.datetime64(stop))]
...
>>>
>>> first_last_range('1970-02-05', '1970-05-08')
array(['1970-02-28', '1970-03-01', '1970-03-31', '1970-04-01',
'1970-04-30'], dtype='datetime64[D]')
請注意,您可能想使端點語義適應您的需求.
總結
以上是生活随笔為你收集整理的python 函数 日期区间_python-如何获取两个日期之间的日期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超级vga显示卡_VGA 显示卡简介
- 下一篇: ue4 开发动作游戏_【图片】第三人称动