日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 函数 日期区间_python-如何获取两个日期之间的日期

發布時間:2025/4/16 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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-如何获取两个日期之间的日期的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。