Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas: Holidays between two dates using the US federal holiday calendar

Pandas Time Series: Exercise-30 with Solution

Write a Pandas program to generate holidays between two dates using the US federal holiday calendar.

Sample Solution:

Python Code :

import pandas as pd
from pandas.tseries.holiday import *
sdt = datetime(2021, 1, 1)
edt = datetime(2030, 12, 31)
print("Holidays between 2021-01-01 and 2030-12-31 using the US federal holiday calendar.")
cal = USFederalHolidayCalendar()
for dt in cal.holidays(start=sdt, end=edt): 
    print (dt)

Sample Output:

Holidays between 2021-01-01 and 2030-12-31 using the US federal holiday calendar.
2021-01-01 00:00:00
2021-01-18 00:00:00
2021-02-15 00:00:00
2021-05-31 00:00:00
2021-07-05 00:00:00
2021-09-06 00:00:00
2021-10-11 00:00:00
2021-11-11 00:00:00
2021-11-25 00:00:00
2021-12-24 00:00:00
2021-12-31 00:00:00
2022-01-17 00:00:00
2022-02-21 00:00:00
2022-05-30 00:00:00
2022-07-04 00:00:00
2022-09-05 00:00:00
2022-10-10 00:00:00
2022-11-11 00:00:00
2022-11-24 00:00:00
2022-12-26 00:00:00
2023-01-02 00:00:00
2023-01-16 00:00:00
2023-02-20 00:00:00
2023-05-29 00:00:00
2023-07-04 00:00:00
2023-09-04 00:00:00
2023-10-09 00:00:00
2023-11-10 00:00:00
2023-11-23 00:00:00
2023-12-25 00:00:00
2024-01-01 00:00:00
2024-01-15 00:00:00
2024-02-19 00:00:00
2024-05-27 00:00:00
2024-07-04 00:00:00
2024-09-02 00:00:00
2024-10-14 00:00:00
2024-11-11 00:00:00
2024-11-28 00:00:00
2024-12-25 00:00:00
2025-01-01 00:00:00
2025-01-20 00:00:00
2025-02-17 00:00:00
2025-05-26 00:00:00
2025-07-04 00:00:00
2025-09-01 00:00:00
2025-10-13 00:00:00
2025-11-11 00:00:00
2025-11-27 00:00:00
2025-12-25 00:00:00
2026-01-01 00:00:00
2026-01-19 00:00:00
2026-02-16 00:00:00
2026-05-25 00:00:00
2026-07-03 00:00:00
2026-09-07 00:00:00
2026-10-12 00:00:00
2026-11-11 00:00:00
2026-11-26 00:00:00
2026-12-25 00:00:00
2027-01-01 00:00:00
2027-01-18 00:00:00
2027-02-15 00:00:00
2027-05-31 00:00:00
2027-07-05 00:00:00
2027-09-06 00:00:00
2027-10-11 00:00:00
2027-11-11 00:00:00
2027-11-25 00:00:00
2027-12-24 00:00:00
2027-12-31 00:00:00
2028-01-17 00:00:00
2028-02-21 00:00:00
2028-05-29 00:00:00
2028-07-04 00:00:00
2028-09-04 00:00:00
2028-10-09 00:00:00
2028-11-10 00:00:00
2028-11-23 00:00:00
2028-12-25 00:00:00
2029-01-01 00:00:00
2029-01-15 00:00:00
2029-02-19 00:00:00
2029-05-28 00:00:00
2029-07-04 00:00:00
2029-09-03 00:00:00
2029-10-08 00:00:00
2029-11-12 00:00:00
2029-11-22 00:00:00
2029-12-25 00:00:00
2030-01-01 00:00:00
2030-01-21 00:00:00
2030-02-18 00:00:00
2030-05-27 00:00:00
2030-07-04 00:00:00
2030-09-02 00:00:00
2030-10-14 00:00:00
2030-11-11 00:00:00
2030-11-28 00:00:00
2030-12-25 00:00:00

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program create a series with a PeriodIndex which represents all the calendar month periods in 2029 and 2031. Also print the values for all periods in 2030.
Next: Write a Pandas program to create a monthly time period and display the list of names in the current local scope.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m