说我有一个日历:
from pandas.tseries.holiday import USFederalHolidayCalendar
cal = USFederalHolidayCalendar()
cal.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>)]
是否有一些简单的方法可以将其转换为带有每个类别列的虚拟df?
即。每个假日的一列,在开始和结束日期之间的每个日期都有bool(或0/1&#39; s),即:
Date NewYearsDay … Christmas
2012-01-01 True … False
2012-01-02 False … False
…
2016-12-31 False … False
猜猜我可以从Holiday.dates
方法开始,如果没有内置逻辑
答案 0 :(得分:2)
为了将来参考,可以使用return_name=True
并使用get_dummies
:
pd.get_dummies(cal.holidays(start='2012-01-01', end='2016-12-31', return_name=True))
哪个收益
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