Python|假期图书馆
Python Holidays 库是一个高效的库,用于尽可能快速灵活地确定特定日期是否为假期。对于任何一个国家,人们都可以知道那一天是否是假期。只能检测到圣诞节、新年等固定日期(公共)假期。
安装:
pip install holidays
句法:
holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)
参数:
years : An iterable list of integers specifying the years that the Holiday object should pre-generate. This would generally only be used if setting expand to False. (Default[])
expand : A boolean value which specifies whether or not to append holidays in new years to the holidays object. (Default: True)
observed : A boolean value which when set to True will include the observed day of a holiday that falls on a weekend, when appropriate. (Default: True)
prov : A string specifying a province that has unique statutory holidays. (Default: Australia=’ACT’, Canada=’ON’, NewZealand=None)
state : A string specifying a state that has unique statutory holidays. (Default: UnitedStates=None)
方法:
- get(key, default=None):返回一个字符串,其中包含日期键中的假期名称,可以是日期、日期时间、字符串、unicode、字节、整数或浮点类型。如果多个假期在同一日期,名称将用逗号分隔。
- get_list(key):与 get 相同,除了返回假日名称列表而不是逗号分隔的字符串。
- pop(key, default=None):与
get
相同,除了从假期对象更新/追加中删除键。接受 {date: name} 对的字典、日期列表,甚至是单数日期/字符串/时间戳对象,并将它们添加到假期列表中。
代码 #1:对于特定国家和年份,显示所有假期。
from datetime import date
import holidays
# Select country
uk_holidays = holidays.UnitedKingdom()
# Print all the holidays in UnitedKingdom in year 2018
for ptr in holidays.UnitedKingdom(years = 2018).items():
print(ptr)
输出:
(datetime.date(2018, 1, 1), "New Year's Day")
(datetime.date(2018, 1, 2), 'New Year Holiday [Scotland]')
(datetime.date(2018, 3, 17), "St. Patrick's Day [Northern Ireland]")
(datetime.date(2018, 3, 19), "St. Patrick's Day [Northern Ireland] (Observed)")
(datetime.date(2018, 3, 30), 'Good Friday')
(datetime.date(2018, 4, 2), 'Easter Monday [England, Wales, Northern Ireland]')
(datetime.date(2018, 5, 7), 'May Day')
(datetime.date(2018, 5, 28), 'Spring Bank Holiday')
(datetime.date(2018, 7, 12), 'Battle of the Boyne [Northern Ireland]')
(datetime.date(2018, 8, 6), 'Summer Bank Holiday [Scotland]')
(datetime.date(2018, 8, 27), 'Late Summer Bank Holiday [England, Wales, Northern Ireland]')
(datetime.date(2018, 11, 30), "St. Andrew's Day [Scotland]")
(datetime.date(2018, 12, 25), 'Christmas Day')
(datetime.date(2018, 12, 26), 'Boxing Day')
代码#2:检查给定日期是否为假期
from datetime import date
import holidays
# Select country
uk_holidays = holidays.UnitedKingdom()
# If it is a holidays then it returns True else False
print('01-01-2018' in uk_holidays)
print('02-01-2018' in uk_holidays)
# What holidays is it?
print(uk_holidays.get('01-01-2018'))
print(uk_holidays.get('02-01-2018'))
输出:
True
False
New Year's Day
None
代码#3:北美假期
from datetime import date
import holidays
# Combining Countries
north_america = holidays.CA() + holidays.US() + holidays.MX()
# Output list of countries combined
print(north_america.country)
print(north_america.get('07-01-2018'))
print(north_america.get('07-04-2018'))
输出:
['CA', 'US', 'MX']
Canada Day
Independence Day
假日图书馆中包含的国家列表 -
Country | Abbr | Provinces/States Available |
---|---|---|
Argentina | AR | None |
Australia | AU | prov = ACT (default), NSW, NT, QLD, SA, TAS, VIC, WA |
Austria | AT | prov = B, K, N, O, S, ST, T, V, W (default) |
Belgium | BE | None |
Canada | CA | prov = AB, BC, MB, NB, NL, NS, NT, NU, ON (default), PE, QC, SK, YU |
Colombia | CO | None |
Czech | CZ | None |
Denmark | DK | None |
England | None | |
EuropeanCentralBank | ECB,TAR | Trans-European Automated Real-time Gross Settlement (TARGET2) |
Finland | FI | None |
France | FRA | Métropole (default), Alsace-Moselle, Guadeloupe, Guyane, Martinique, Mayotte, Nouvelle-Calédonie, La Réunion, Polynésie Française, Saint-Barthélémy, Saint-Martin, Wallis-et-Futuna |
Germany | DE | BW, BY, BE, BB, HB, HH, HE, MV, NI, NW, RP, SL, SN, ST, SH, TH |
Hungary | HU | None |
Ireland | IE | |
Isle of Man | None | |
Italy | IT | prov = MI, RM |
Japan | JP | None |
Mexico | MX | None |
Netherlands | NL | None |
NewZealand | NZ | prov = NTL, AUK, TKI, HKB, WGN, MBH, NSN, CAN, STC, WTL, OTA, STL, CIT |
Northern Ireland | None | |
Norway | NO | None |
Polish | PL | None |
Portugal | PT | None |
PortugalExt | PTE | Portugal plus extended days most people have off |
Scotland | None | |
Slovenia | SI | None |
Slovakia | SK | None |
South Africa | ZA | None |
Spain | ES | prov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL, IBA, ICA, MAD, MUR, NAV, PVA, RIO |
Sweden | SE | None |
Switzerland | CH | prov = AG, AR, AI, BL, BS, BE, FR, GE, GL, GR, JU, LU, NE, NW, OW, SG, SH, SZ, SO, TG, TI, UR, VD, VS, ZG, ZH |
UnitedKingdom | UK | None |
UnitedStates | US | state = AL, AK, AS, AZ, AR, CA, CO, CT, DE, DC, FL, GA, GU, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MH, MA, MI, FM, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, MP, OH, OK, OR, PW, PA, PR, RI, SC, SD, TN, TX, UT, VT, VA, VI, WA, WV, WI, WY |
Wales | None |
在这个图书馆里,很多国家都不见了。因此,我们可以制作自己的自定义假期。
代码 #4:为印度添加自定义假期
from datetime import date
import holidays
in_holidays = holidays.HolidayBase()
# Let's check our republic day
print('26-01-2019' in in_holidays)
# Add Holiday without description
in_holidays.append('26-01-2019')
# Let's verify
print('26-01-2019' in in_holidays) # True
# Let's Check Description
print(in_holidays.get('26-01-2019'))
# Add Holiday with description
in_holidays.append({'26-01-2019':'Republic Day India'})
print(in_holidays.get('26-01-2019'))
# Add list of Dates Together
in_holidays.append(['02-10-2018', '15-08-2018'])
print('15-08-2018' in in_holidays) # True
print('02-10-2018' in in_holidays) # True
# a single date item
in_holidays.append(date(2018, 12, 25))
print('25-12-2018' in in_holidays) # True
输出:
False
True
Holiday
Republic Day India, Holiday
True
True
True
参考: https://pypi.org/project/holidays/