Python | Holidays library - GeeksforGeeks (2024)

Last Updated : 17 Jul, 2018

Improve

Python Holidays library is an efficient library for determining whether a specific date is a holiday as fast and flexible as possible. For any country, one can find whether that day is a holiday or not. Only fixed days(Public) holidays like Christmas, New Year, etc. can be detected.

Installation:

pip install holidays

Syntax:

holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)

Parameters:

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)

Methods:

  • get(key, default=None): Returns a string containing the name of the holiday(s) in date key, which can be of date, datetime, string, unicode, bytes, integer or float type. If multiple holidays fall on the same date the names will be separated by commas.
  • get_list(key): Same as get except returns a list of holiday names instead of a comma-separated string.
  • pop(key, default=None): Same as get, except the key is removed from the holiday object update/append. Accepts dictionary of {date: name} pairs, a list of dates, or even singular date/string/timestamp objects and adds them to the list of holidays.

Code #1 : For a Particular Country and Year display all Holidays.

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)

Output:

(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')


Code #2 : Check whether a given date is holiday or not

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'))

Output:

TrueFalseNew Year's DayNone


Code #3 : Holidays in North America

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'))

Output:

['CA', 'US', 'MX']Canada DayIndependence Day

List of Countries included in Holiday Library –

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
England None
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
Ireland IE
Isle of Man None
ItalyITprov = MI, RM
JapanJPNone
MexicoMXNone
NetherlandsNLNone
NewZealandNZprov = NTL, AUK, TKI, HKB, WGN, MBH, NSN, CAN, STC, WTL,
OTA, STL, CIT
Northern Ireland None
NorwayNONone
PolishPLNone
PortugalPTNone
PortugalExtPTEPortugal plus extended days most people have off
Scotland None
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
Wales None

In this Library, many countries are missing. So, we can make own Custom Holidays.

Code #4 : Custom Holidays adding for India

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


Output:

False True Holiday Republic Day India, HolidayTrue True True 


Reference: https://pypi.org/project/holidays/



Like Article

Suggest improvement

Previous

Understanding Python Pickling with example

Next

numpy.polyadd() in Python

Share your thoughts in the comments

Please Login to comment...

Python | Holidays library - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 5808

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.