Shoichi Takasaki
Shoichi Takasaki

Reputation: 21

How to exact holiday names from Pandas USFederalHolidayCalendar?

I would like to extract US Holiday names (e.g. "Memorial Day") using USFederalHolidayCalendar in Pandas library. The code below is just printing US holidays in the given range. I don't necessarily need to use Pandas for this purpose though if there is an easier way.

cal=USFederalHolidayCalendar()
y_str=datetime.datetime.now().strftime("%Y")
holidays = cal.holidays(start=y_str+'-01-01', end=y_str+'-12-31')

for h in holidays:
    print(h)

I know that "cal.rules" can return a list like below. Should I extract from this? If so, this doesn't look like a list of strings. What is the content type of list?

[Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x0000012EF1B3B280>), Holiday: Martin Luther King Jr. Day (month=1, day=1, offset=<DateOffset: weekday=MO(+3)>), Holiday: Presidents Day (month=2, da

Upvotes: 1

Views: 3667

Answers (1)

BrokenBenchmark
BrokenBenchmark

Reputation: 19242

cal.rules gives a list of pandas.tseries.holiday.Holiday objects. These objects have .name attributes (see source). So, you can do the following:

from pandas.tseries.holiday import USFederalHolidayCalendar
import datetime

cal = USFederalHolidayCalendar()
holidays = cal.rules

print([holiday.name for holiday in holidays])

Upvotes: 3

Related Questions