📜  Python|假期图书馆

📅  最后修改于: 2022-05-13 01:55:22.686000             🧑  作者: Mango

Python|假期图书馆

Python Holidays 库是一个高效的库,用于尽可能快速灵活地确定特定日期是否为假期。对于任何一个国家,人们都可以知道那一天是否是假期。只能检测到圣诞节、新年等固定日期(公共)假期。

安装:

pip install holidays

句法:

参数:

方法:

  • 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

假日图书馆中包含的国家列表 -

CountryAbbrProvinces/States Available
ArgentinaARNone
AustraliaAUprov = ACT (default), NSW, NT, QLD, SA, TAS, VIC, WA
AustriaATprov = B, K, N, O, S, ST, T, V, W (default)
BelgiumBENone
CanadaCAprov = AB, BC, MB, NB, NL, NS, NT, NU, ON (default),
PE, QC, SK, YU
ColombiaCONone
CzechCZNone
DenmarkDKNone
EnglandNone
EuropeanCentralBankECB,TARTrans-European Automated Real-time Gross Settlement (TARGET2)
FinlandFINone
FranceFRAMé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
GermanyDEBW, BY, BE, BB, HB, HH, HE, MV, NI, NW, RP, SL, SN, ST,
SH, TH
HungaryHUNone
IrelandIE
Isle of ManNone
ItalyITprov = MI, RM
JapanJPNone
MexicoMXNone
NetherlandsNLNone
NewZealandNZprov = NTL, AUK, TKI, HKB, WGN, MBH, NSN, CAN, STC, WTL,
OTA, STL, CIT
Northern IrelandNone
NorwayNONone
PolishPLNone
PortugalPTNone
PortugalExtPTEPortugal plus extended days most people have off
ScotlandNone
SloveniaSINone
SlovakiaSKNone
South AfricaZANone
SpainESprov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL,
IBA, ICA, MAD, MUR, NAV, PVA, RIO
SwedenSENone
SwitzerlandCHprov = 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
UnitedKingdomUKNone
UnitedStatesUSstate = 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
WalesNone

在这个图书馆里,很多国家都不见了。因此,我们可以制作自己的自定义假期。

代码 #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/