我正在尝试使用Python 3.6中的静态方法创建一个类USTradingCalendar
。
问题:但是当我尝试调用静态方法get_trading_close_holidays()
时,出现错误
AttributeError: 'datetime.datetime' object has no attribute 'rules'
出了什么问题?任何帮助表示赞赏!
import datetime as dt
from helpers import str2date
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)
]
@classmethod
def get_trading_close_holidays(cls, year):
return cls.holidays(dt.datetime(year-1, 12, 31), dt.datetime(year, 12, 31))
if __name__ == '__main__':
print(USTradingCalendar.get_trading_close_holidays(2016))
回溯:
Traceback (most recent call last):
File "tradingCalendar.py", line 37, in <module>
print(USTradingCalendar.get_trading_close_holidays(2016))
File "tradingCalendar.py", line 32, in get_trading_close_holidays
return cls.holidays(dt.datetime(year-1, 12, 31), dt.datetime(year, 12, 31))
File "C:\Users\y\Anaconda2\envs\test\lib\site-packages\pandas\tseries\holiday.py", line 376, in holidays
if self.rules is None:
AttributeError: 'datetime.datetime' object has no attribute 'rules'
答案 0 :(得分:2)
holidays()
需要AbstractHolidayCalendar
类型的参数,因此请将其更改为:
@classmethod
def get_trading_close_holidays(cls, year):
return cls.holidays(USTradingCalendar(), dt.datetime(year-1, 12, 31), dt.datetime(year, 12, 31))