【发布时间】:2016-02-15 16:26:07
【问题描述】:
我有以下代码用于市场假期的自定义偏移类。出于某种原因,虽然我不能减去偏移量。添加工作正常。有人可以发现错误吗?
from pandas.tseries.offsets import CustomBusinessDay
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
class Holiday_offset:
'''
creates an offset for market holidays so that the object that is returned
may be added or subtracted to a datetime object to move 1 day forward or
back account for holidays and business days.
'''
def __init__(self):
self.cal = get_calendar('USFederalHolidayCalendar') # Create calendar instance
self.cal.rules.pop(7) # Remove Veteran's Day rule
self.cal.rules.pop(6) # Remove Columbus Day rule
self.tradingCal = HolidayCalendarFactory('TradingCalendar', self.cal, GoodFriday)
#new instance of class return as an offset object
self.offset = CustomBusinessDay(calendar=self.tradingCal())
def __add__(self,other):
return self.offset + other
def __radd__(self,other):
return self.offset + other
def __iadd__(self,other):
return self.offset + other
def __sub__(self,other):
return self.offset - other
def __rsub__(self,other):
return self.offset - other
def __isub__(self,other):
return self.offset - other
def __index__(self,other):
TypeError: Cannot subtract datetime from offset
【问题讨论】:
-
你说的是iteration protocol吗?
-
不,因为即使我只是访问偏移元素,我也会从该方法而不是我的自定义类中得到错误。
-
很抱歉忘记更改问题的标题,这可能会导致您的困惑。
标签: python pandas calendar overriding offset