Home > Back-end >  Generate holiday data using python holidays library
Generate holiday data using python holidays library

Time:01-12

I want to create a datafarme with the holidays using the python holiday library.

from datetime import date
import holidays
us_holidays = holidays.US() 
date(2015, 1, 1) in us_holidays  # True
date(2015, 1, 2) in us_holidays  # False

I want to create a dataframe with dates and holiday column. For example I want to create a data frame of holidays from 01.01.2019 to 31.12.2020

Expected Output:

date           holiday
01.01.2019       1
09.03.2019       1
.
.
.
31.12.2020       1

How can I extract this dataframe from the holiday package?

CodePudding user response:

There is a country_holidays function:

import pandas as pd
import holidays
data = ((date, 1) for date in holidays.country_holidays('US', years=2023))
df = pd.DataFrame(data, columns =['date', 'holiday'])
df.head()

the output is

         date  holiday
0  2023-01-01        1
1  2023-01-02        1
2  2023-01-16        1
3  2023-02-20        1
4  2023-05-29        1

CodePudding user response:

Hops this helps you

from datetime import date
import holidays
import pandas as pd
lst = []
us_holidays = holidays.US()
start_date = date(2023, 1, 1)
end_date = date(2023, 12, 31)
delta = timedelta(days=1)
while start_date <= end_date:
  if start_date.strftime("%Y-%m-%d") in us_holidays:
    lst.append([start_date.strftime("%Y-%m-%d"),1])
  start_date  = delta
df = pd.DataFrame(lst, columns=['Date','Holiday'])

Output

    Date        Holiday
0   2023-01-01  1
1   2023-01-02  1
2   2023-01-16  1
3   2023-02-20  1
4   2023-05-29  1
5   2023-06-19  1
6   2023-07-04  1
7   2023-09-04  1
8   2023-10-09  1
9   2023-11-10  1
10  2023-11-11  1
11  2023-11-23  1
12  2023-12-25  1
  • Related