Example #1
0
    def __call__(self,ts,*args,**kwargs):
        """Turning Point - fill a series with a value from a turning point

        :param tsl: the input time-series
        :type tsl: a time-series
        :param D: a date specification
        :type D: a string representing a date (YYYY for A, YYYYMM for M, YYYYMMDD for D)
        :param V: a value (default 0.0)
        :type V: float

        :return: the modified time-series
        :rtype: a time-series

        :Example:
        dstream://Datastream/1.0$M_LIMIT?name=SOMEONE&proc=turningp(201101)
        dstream://Datastream/0.0$M_LIMIT?name=SOMEZERO&proc=turningp(201101,1.0)
        """
        V=0.0
        D="today"
        invert = False
        if len(args)==1:
            D = args[0]
        if len(args)==2:
            V=float(args[1])
        if len(args)==3:
            invert=bool(args[2])
        if 'VALUE' in kwargs:
            V=float(kwargs['VALUE'])
        if 'INVERT' in kwargs:
            invert = bool(kwargs['INVERT'])
        freq = ts.index.freq
        if re.match('^today$',D,re.I): 
            _tp = datetime.now() 
        else:         
            if freq == 'A':
                _Y = int(D[0:4])
                _tp = Period(freq=freq,value=_Y)
            elif freq == 'M':
                _Y = int(D[0:4])
                _M = int(D[4:6])
                _tp = Period(freq=freq,year=_Y,month=_M)
            elif freq == 'Q':
                _Y = int(D[0:4])
                _Q = int(D[5:6])
                _tp = Period(freq=freq,year=_Y,quarter=_Q)
            elif freq == 'D':
                _Y = int(D[0:4])
                _M = int(D[4:6])
                _D = int(D[6:8])
                _tp = Period(freq=freq,year=_Y,month=_M,day=_D)
            else:
                raise OperationError, "turningp"
        if isinstance(V,basestring):
            if re.match('^NAN$',V,re.I): 
                V=np.nan 
            else: 
                V = float(V) 
        _t1 = Series(data=ts,copy=True)
        _t1.ffill(0.0)
        start = _tp
        end = ts.index[-1]        
        if invert:
            start = ts.index[0]
            end = _tp
        _tpd = period_range(str(start),str(end),freq=freq)
        _t1.fillna(method='bfill')
        for _p in _tpd:
            _t1[_p]=1.0
        _t = ts * _t1
        _t2 = Series(ts,copy=True)
        _t2.fill(V)
        _tpd = period_range(start,end,freq=freq)
        for _p in _tpd:
            _t1[_p]=0.0
        _tx = _t + _t2
        return _tx