简体   繁体   中英

get_dummies from Holiday objects in pandas

Say I have a calendar:

from pandas.tseries.holiday import USFederalHolidayCalendar
cal = USFederalHolidayCalendar()
cal.rules

with rules:

[Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x1164d3268>),
 Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
 Holiday: Presidents Day (month=2, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
 Holiday: MemorialDay (month=5, day=31, offset=<DateOffset: kwds={'weekday': MO(-1)}>),
 Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x1164d3268>),
 Holiday: Labor Day (month=9, day=1, offset=<DateOffset: kwds={'weekday': MO(+1)}>),
 Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: kwds={'weekday': MO(+2)}>),
 Holiday: Veterans Day (month=11, day=11, observance=<function nearest_workday at 0x1164d3268>),
 Holiday: Thanksgiving (month=11, day=1, offset=<DateOffset: kwds={'weekday': TH(+4)}>),
 Holiday: Christmas (month=12, day=25, observance=<function nearest_workday at 0x1164d3268>)]

Is there some easy way to turn this into a dummy df with a column for each category?

Ie a column for each holiday with bools (or 0/1's) for each date between the start and stop date, ie:

Date           NewYearsDay        …         Christmas
2012-01-01     True               …         False
2012-01-02     False              …         False
…
2016-12-31     False              …         False

Guess I can work my way from the Holiday.dates -method, if there is no built-in logic for this

For future reference, one can use return_name=True and use get_dummies on that:

pd.get_dummies(cal.holidays(start='2012-01-01', end='2016-12-31', return_name=True))

Which yields

            Christmas   Columbus Day    Dr. Martin Luther King Jr.  July 4th …
2014-01-01  0           0               0                           0   
2014-01-20  0           0               1                           0   
2014-02-17  0           0               0                           0   
2014-05-26  0           0               0                           0   
2014-07-04  0           0               0                           1   
2014-09-01  0           0               0                           0   

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM