2015-10-13 73 views
13

我正在嘗試使用熊貓創建交易日曆。我能夠基於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) 

任何想法?

回答

22

也許是更直接地從頭開始創建貿易日曆,就像這樣:

import datetime as dt 

from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday, \ 
    USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, \ 
    USLaborDay, USThanksgivingDay 


class USTradingCalendar(AbstractHolidayCalendar): 
    rules = [ 
     Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday), 
     USMartinLutherKingJr, 
     USPresidentsDay, 
     GoodFriday, 
     USMemorialDay, 
     Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday), 
     USLaborDay, 
     USThanksgivingDay, 
     Holiday('Christmas', month=12, day=25, observance=nearest_workday) 
    ] 


def get_trading_close_holidays(year): 
    inst = USTradingCalendar() 

    return inst.holidays(dt.datetime(year-1, 12, 31), dt.datetime(year, 12, 31)) 


if __name__ == '__main__': 
    print(get_trading_close_holidays(2016)) 
    # DatetimeIndex(['2016-01-01', '2016-01-18', '2016-02-15', '2016-03-25', 
    #     '2016-05-30', '2016-07-04', '2016-09-05', '2016-11-24', 
    #     '2016-12-26'], 
    #     dtype='datetime64[ns]', freq=None) 
6

您必須創建新的類實例:cal1 = tradingCal()。這對我有用。

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) 
print tradingCal.rules 

#new instance of class 
cal1 = tradingCal() 

print cal1.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31)) 

#DatetimeIndex(['2015-01-01', '2015-01-19', '2015-02-16', '2015-04-03', 
#    '2015-05-25', '2015-07-03', '2015-09-07', '2015-11-26', 
#    '2015-12-25', '2016-01-01', '2016-01-18', '2016-02-15', 
#    '2016-03-25', '2016-05-30', '2016-07-04', '2016-09-05', 
#    '2016-11-24', '2016-12-26'], 
#    dtype='datetime64[ns]', freq=None, tz=None) 
+0

完美,jezrael。謝謝。 – vlmercado

+4

我認爲這有一個重大的缺點!使用'.pop'會影響底層類「pandas.tseries.holiday.USFederalHolidayCalendar」,因爲它是「就地」操作(不確定術語)。這意味着如果您嘗試從'get_calendar('USFederalHolidayCalendar')'重新創建cal2,則規則與'cal'相同。即你沒有得到* Clean *版本的'USFederalHolidayCalendar',因爲它不再存在,你修改了它! – evan54

+0

@ evan54我剛剛也注意到了這一點。你有解決方案嗎? – WillZ

8

如果有幫助,我對外匯交易日曆同樣需要。 Quantopian在Zipline項目中埋藏了一些優秀的代碼。我提取出相關部分,並創建了一個新的項目,用於創建大熊貓市場交易交易日曆。鏈接在這裏,具有下面描述的一些功能。

https://github.com/rsheftel/pandas_market_calendars

https://pypi.python.org/pypi/pandas-market-calendars

下面是它可以通過創建所有的有效開放時間爲紐約證券交易所的大熊貓DatetimeIndex做:

import pandas_market_calendars as mcal 
nyse = mcal.get_calendar('NYSE') 

early = nyse.schedule(start_date='2012-07-01', end_date='2012-07-10') 
early 

        market_open    market_close 
=========== ========================= ========================= 
2012-07-02 2012-07-02 13:30:00+00:00 2012-07-02 20:00:00+00:00 
2012-07-03 2012-07-03 13:30:00+00:00 2012-07-03 17:00:00+00:00 
2012-07-05 2012-07-05 13:30:00+00:00 2012-07-05 20:00:00+00:00 
2012-07-06 2012-07-06 13:30:00+00:00 2012-07-06 20:00:00+00:00 
2012-07-09 2012-07-09 13:30:00+00:00 2012-07-09 20:00:00+00:00 
2012-07-10 2012-07-10 13:30:00+00:00 2012-07-10 20:00:00+00:00 

mcal.date_range(early, frequency='1D') 

DatetimeIndex(['2012-07-02 20:00:00+00:00', '2012-07-03 17:00:00+00:00', 
       '2012-07-05 20:00:00+00:00', '2012-07-06 20:00:00+00:00', 
       '2012-07-09 20:00:00+00:00', '2012-07-10 20:00:00+00:00'], 
       dtype='datetime64[ns, UTC]', freq=None) 

mcal.date_range(early, frequency='1H') 

DatetimeIndex(['2012-07-02 14:30:00+00:00', '2012-07-02 15:30:00+00:00', 
       '2012-07-02 16:30:00+00:00', '2012-07-02 17:30:00+00:00', 
       '2012-07-02 18:30:00+00:00', '2012-07-02 19:30:00+00:00', 
       '2012-07-02 20:00:00+00:00', '2012-07-03 14:30:00+00:00', 
       '2012-07-03 15:30:00+00:00', '2012-07-03 16:30:00+00:00', 
       '2012-07-03 17:00:00+00:00', '2012-07-05 14:30:00+00:00', 
       '2012-07-05 15:30:00+00:00', '2012-07-05 16:30:00+00:00', 
       '2012-07-05 17:30:00+00:00', '2012-07-05 18:30:00+00:00', 
       '2012-07-05 19:30:00+00:00', '2012-07-05 20:00:00+00:00', 
       '2012-07-06 14:30:00+00:00', '2012-07-06 15:30:00+00:00', 
       '2012-07-06 16:30:00+00:00', '2012-07-06 17:30:00+00:00', 
       '2012-07-06 18:30:00+00:00', '2012-07-06 19:30:00+00:00', 
       '2012-07-06 20:00:00+00:00', '2012-07-09 14:30:00+00:00', 
       '2012-07-09 15:30:00+00:00', '2012-07-09 16:30:00+00:00', 
       '2012-07-09 17:30:00+00:00', '2012-07-09 18:30:00+00:00', 
       '2012-07-09 19:30:00+00:00', '2012-07-09 20:00:00+00:00', 
       '2012-07-10 14:30:00+00:00', '2012-07-10 15:30:00+00:00', 
       '2012-07-10 16:30:00+00:00', '2012-07-10 17:30:00+00:00', 
       '2012-07-10 18:30:00+00:00', '2012-07-10 19:30:00+00:00', 
       '2012-07-10 20:00:00+00:00'], 
       dtype='datetime64[ns, UTC]', freq=None) 

如果你只是想獲得熊貓假期日曆,可以在把它看作一個參數其他熊貓功能一起使用:

holidays = nyse.holidays() 

holidays.holidays[-5:] 
(numpy.datetime64('2030-05-27'), 
numpy.datetime64('2030-07-04'), 
numpy.datetime64('2030-09-02'), 
numpy.datetime64('2030-11-28'), 
numpy.datetime64('2030-12-25')) 
+0

此庫不可靠。當我嘗試它時,2010-01-01和其他新年日曆在星期五以及其他星期五出現。 –

+2

您可能未正確使用該軟件包。 2010-01-01和所有其他新年和聖誕節的星期五不包括在交易日。試試上面的示例代碼。如果您想了解如何使用包,我建議在線文檔或者你可以留言給我,直接,如果你仍然有問題。 –

+0

你說得對。我對照自1995年以來測試的日曆進行了檢查,結果是準確的。不記得我用什麼語法,給了我不想要的結果。 –