I have the following dataframe:
| ID | date |
|--------------|-----------------------------------------|
| 0 | 2022-01-01 12:00:01+05:00 |
| 1 | 2022-01-30 21:30:01+03:00 |
| 2 | 2022-02-15 13:04:02+02:00 |
| 3 | 2022-09-05 15:30:01+00:00 |
| 4 | 2022-04-21 13:18:02+02:00 |
The date column is a python TimeStamp. I am using the python holidays library, I would like to use the following code:
from datetime import date
import holidays
usa_holidays = holidays.country_holidays('US')
texas_holidays = holidays.country_holidays('US', subdiv='TX')
florida_holidays = holidays.country_holidays('US', subdiv='FL')
california_holidays = holidays.country_holidays('US', subdiv='CA')
# df is the dataframe above
# It doesn't work.
df['only_date'] = df['date'].apply(lambda x: x.date())
df['federal_holiday'] = df['only_date'].isin(usa_holidays)
# Returns holiday name 'New Year's Day'
print(usa_holidays.get('2022-01-01'))
I would like to add the following columns:
- federal_holiday: True or False if the day is a bank holiday (country dictionary).
- holiday_state: True if it is a holiday in at least one of the state-related dictionaries. False in other case.
- name_state: names of the states in which that day is a public holiday, if it falls on all days, write all.
- holiday_name: Name of the festival.
The resulting dataframe would look as follows:
| ID | date | federal_holiday | holiday_state | name_state | holiday_name |
|----|---------------------------|-----------------|---------------|------------|----------------------|
| 0 | 2022-01-01 12:00:01+05:00 | True | True | all | New Year's Day |
| 1 | 2022-01-30 21:30:01+03:00 | False | False | NaN | NaN |
| 2 | 2022-02-15 13:04:02+02:00 | False | True | FL,CA | Susan B. Anthony Day |
| 3 | 2022-09-05 15:30:01+00:00 | True | True | all | Labor Day |
| 4 | 2022-04-21 13:18:02+02:00 | False | True | TX | San Jacinto Day |