Numpy 的 busday_count(~)
方法計算給定日期時間範圍的有效日期的數量。請注意,busday 代表工作日,但我們可以通過參數指定"business" 日。
參數
1.begindates
| array-like
共 datetime
開始日期時間。日期時間本質上是格式化的字符串,如下所示:
"2020-05-25"
2. enddates
| array-like
共 datetime
結束日期時間。這是排他性的,也就是說,如果您指定 enddates=A
並且您的某個日期確實包含 A,則該日期將不會包含在計數中。
3. weekmask
| string
或array-like
或boolean
| optional
一周中被視為有效的日子。您可以指定長度為 7 的字符串,其中 0 代表無效,1 代表有效工作日,從周一到周日。例如,"1111100"
意味著周末(周六和周日)為無效日期。此外,您還可以使用 three-character 縮寫,例如:
Mon Tue Wed Thu Fri Sat Sun
例如,"Mon Wed Fri"
表示隻有星期一、星期三和星期五是有效日期,所有其他工作日均無效。
或者,您可以提供大小為 7 的布爾值數組,其中 True 表示相應的工作日有效,否則為 False。例如,[True,True,True,True,True,False,False]
再次意味著周末是無效日期。
默認情況下, weekmask="1111100"
,即有效工作日為周一至周五(含周一至周五)。
4. holidays
| datetime
的array-like
| optional
被視為無效日期的日期時間數組。
5. busdaycal
| busdaycalender
| optional
一個 Busdaycalender 對象,指定哪些日期被視為有效日期。如果提供了此參數,則不應指定參數 weekmask 和holidays。
6. out
| Numpy array
| optional
我們可以將結果存儲在 out
中,這樣可以節省內存空間,因為不會創建新的數組。
返回值
如果為 begindates
和 enddates
提供了日期時間,則返回單個整數。一個 Numpy 整數數組,表示指定範圍內的日期計數。
例子
基本用法
統計2020-12-20至2020-12-28期間非周一和周日的天數:
np.busday_count("2020-12-20", "2020-12-28", "0111110")
5
計算2020-12-20至2020-12-28期間除2020-12-25和2020-12-27之外的天數:
christmas_break = ["2020-12-25", "2020-12-27"]
np.busday_count("2020-12-20", "2020-12-28", holidays= christmas_break)
4
使用busdaycal對象
計算 2020-12-20 至 2020-12-28 期間星期一和星期二的日期數量:
bdc = np.busdaycalendar(weekmask="Mon Tue")
np.busday_count("2020-12-20", "2020-12-28", busdaycal=bdc)
2
相關用法
- Python NumPy busday_offset方法用法及代碼示例
- Python NumPy busdaycalendar對象用法及代碼示例
- Python MongoDB bulk_write()用法及代碼示例
- Python binascii.crc32用法及代碼示例
- Python bytes.isupper用法及代碼示例
- Python base64.b64decode()用法及代碼示例
- Python bokeh.plotting.figure.asterisk()用法及代碼示例
- Python bytes.zfill用法及代碼示例
- Python binary轉string用法及代碼示例
- Python base64.b32decode()用法及代碼示例
- Python bytes.expandtabs用法及代碼示例
- Python bytearray()用法及代碼示例
- Python bokeh.plotting.figure.annular_wedge()用法及代碼示例
- Python NumPy base屬性用法及代碼示例
- Python Pandas bdate_range方法用法及代碼示例
- Python bytes.isalpha用法及代碼示例
- Python base64.b85encode()用法及代碼示例
- Python base64.b64encode()用法及代碼示例
- Python bokeh.plotting.figure.circle()用法及代碼示例
- Python bytes.hex用法及代碼示例
- Python bool()用法及代碼示例
- Python bz2.decompress(s)用法及代碼示例
- Python bokeh.plotting.figure.circle_cross()用法及代碼示例
- Python bokeh.plotting.figure.bezier()用法及代碼示例
- Python bokeh.plotting.figure.diamond()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | busday_count method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。