1

I have the following code for a custom offset class of market holidays. For some reason though I can not subtract an offset. Addition works fine. Can someone spot the error?

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
Mike
  • 197
  • 1
  • 2
  • 10
  • are you talking about the [iteration protocol](http://stackoverflow.com/questions/16301253/what-exactly-is-pythons-iterator-protocol)? – Pynchia Feb 15 '16 at 16:33
  • No because even if i just access the offset element i get an error from that method not my custom class. – Mike Feb 15 '16 at 16:38
  • sorry forgot to change the title of the question which may have lead to your confusion. – Mike Feb 15 '16 at 16:41

1 Answers1

0

This may be a little bit hacky but I changed the following:

from pandas.tseries.offsets import CustomBusinessDay, BDay


def __sub__(self,other):
    other-= BDay()
    while self.is_holiday(other):
        other -= BDay()
    return other
def __rsub__(self,other):
    return self.__sub__(other)
def __isub__(self,other):
    return self.__sub__(other)
Mike
  • 197
  • 1
  • 2
  • 10