Numpy 的 is_busday(~)
方法檢查每個日期字符串是否是有效日期。請注意,busday 代表工作日,但我們可以通過參數指定"business" 日。
參數
1.dates
| array-like
共 datetime
日期時間的輸入數組。日期時間本質上是格式化的字符串,如下所示:
"2020-05-25"
2. 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"
,即有效工作日為周一至周五(含周一至周五)。
3. holidays
| datetime
的array-like
| optional
被視為無效日期的日期時間數組。
4. busdaycal
| busdaycalender
| optional
一個 busdaycalendar
對象,指定哪些日期被視為有效日期。如果提供此參數,則不應指定參數 weekmask
和 holidays
。
返回值
布爾值的 Numpy 數組,其中 True 表示有效的日期時間,否則為 False。
例子
指定周掩碼
np.is_busday(["2020-12-25", "2020-12-26", "2020-12-27"], "1111110")
array([ True, True, False])
在這裏,我們將星期日設置為無效日期 - 如果您檢查日曆,您會看到 "2020-12-27"
是星期日,因此這就是我們為該條目得到 False 的原因。
指定假期
holidays = ["2020-12-25"]
np.is_busday(["2020-12-25", "2020-12-26", "2020-12-27"], "1111111", holidays)
array([False, True, True])
此處,2020-12-25 返回 False,因為我們將該日期指定為假期,假期被視為無效日期。
指定有效日期
我們還可以隻指定有效日期並使所有其他日期無效,而不是指定無效日期:
bdc = np.busdaycalendar(weekmask="1111111", holidays=["2020-12-26"])
np.is_busday(["2020-12-25", "2020-12-26", "2020-12-27"], busdaycal=bdc)
array([ True, False, True])
此處,busdaycalendar
對象指定哪些日期被視為有效。這裏可能會令人困惑,但 busdaycalender
的構造函數中使用的假期是有效日期時間的列表。
相關用法
- Python isinstance方法用法及代碼示例
- Python string isidentifier()用法及代碼示例
- Python calendar isleap()用法及代碼示例
- Python math isclose()用法及代碼示例
- Python NumPy isalnum方法用法及代碼示例
- Python NumPy isnat方法用法及代碼示例
- Python string isupper()用法及代碼示例
- Python string isalnum()用法及代碼示例
- Python Pandas isnull方法用法及代碼示例
- Python isdisjoint()用法及代碼示例
- Python NumPy isposinf方法用法及代碼示例
- Python issubclass()用法及代碼示例
- Python NumPy isreal方法用法及代碼示例
- Python string istitle()用法及代碼示例
- Python NumPy isclose方法用法及代碼示例
- Python math isnan()用法及代碼示例
- Python NumPy iscomplexobj方法用法及代碼示例
- Python string isalpha()用法及代碼示例
- Python NumPy isnumeric方法用法及代碼示例
- Python NumPy isrealobj方法用法及代碼示例
- Python NumPy isfinite方法用法及代碼示例
- Python string isdigit()用法及代碼示例
- Python NumPy isalpha方法用法及代碼示例
- Python string isdecimal()用法及代碼示例
- Python NumPy isinf方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | is_busday method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。