我正在嘗試使用熊貓創建交易日曆。我能夠基於USFederalHolidayCalendar創建一個cal實例。美國聯邦假日日曆不符合交易日曆,因爲交易日曆不包括哥倫布日和退伍軍人節。但是,交易日曆包括耶穌受難日(不包括在美國聯邦假日日曆中)。除了在下面的代碼的最後一行一切工作:用熊貓創建交易假期日曆
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
from datetime import datetime
cal = get_calendar('USFederalHolidayCalendar') # Create calendar instance
cal.rules.pop(7) # Remove Veteran's Day rule
cal.rules.pop(6) # Remove Columbus Day rule
tradingCal = HolidayCalendarFactory('TradingCalendar', cal, GoodFriday)
的tradingCal實例似乎工作在我能夠查看假日規則。
In[10]: tradingCal.rules
Out[10]:
[Holiday: Labor Day (month=9, day=1, offset=<DateOffset: kwds={'weekday': MO(+1)}>),
Holiday: Presidents Day (month=2, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: Good Friday (month=1, day=1, offset=[<Easter>, <-2 * Days>]),
Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Thanksgiving (month=11, day=1, offset=<DateOffset: kwds={'weekday': TH(+4)}>),
Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Christmas (month=12, day=25, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: MemorialDay (month=5, day=31, offset=<DateOffset: kwds={'weekday': MO(-1)}>)]
當我嘗試列出假期的日期範圍,我得到以下錯誤:
In[11]: tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-12-2708cd2db7a0>", line 1, in <module>
tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
TypeError: unbound method holidays() must be called with TradingCalendar instance as first argument (got datetime instance instead)
任何想法?
完美,jezrael。謝謝。 – vlmercado
我認爲這有一個重大的缺點!使用'.pop'會影響底層類「pandas.tseries.holiday.USFederalHolidayCalendar」,因爲它是「就地」操作(不確定術語)。這意味着如果您嘗試從'get_calendar('USFederalHolidayCalendar')'重新創建cal2,則規則與'cal'相同。即你沒有得到* Clean *版本的'USFederalHolidayCalendar',因爲它不再存在,你修改了它! – evan54
@ evan54我剛剛也注意到了這一點。你有解決方案嗎? – WillZ