Example #1
0
    def _do_add(self, time, value):
        assert value > 0, "Value must be greater than 0"

        if (self.unit == self._generic_unit
                and not isinstance(time, (pd.Timestamp, datetime))):
            return time + value
        elif self.unit not in [self._Observations]:
            return add_td(time, value, self.unit)

        assert self.data is not None, "Must call timedelta with data"

        all_times = self.data[self.data > time].tolist()
        if self.unit == self._Observations:
            if len(all_times) < value:
                raise NotEnoughData()

            return all_times[value - 1]

        raise Exception("Invalid unit")
Example #2
0
    def _do_sub(self, time, value):
        assert value > 0, "Value must be greater than 0"

        if (self.unit == self._generic_unit
                and not isinstance(time, (pd.Timestamp, datetime))):
            return time - value
        elif self.unit != self._Observations:
            return add_td(time, -1 * value, self.unit)

        assert self.data is not None, "Must call timedelta with data"

        # reverse because we want to count backwards when we subtract
        if self.inclusive:
            all_times = self.data[self.data <= time].tolist()[::-1]
        else:
            all_times = self.data[self.data < time].tolist()[::-1]
        if self.unit == self._Observations:
            if len(all_times) < value:
                raise NotEnoughData()
            return all_times[value - 1]

        raise Exception("Invalid unit")