def convert_and_fill(series, freq, func=None, position='END', *args, **kwargs): """ Converts a series from one frequency to another, by manipulating both the `data` and `dates` attributes. This function replicates the standard :func:`scikits.timeseries.convert` function. When converting to a higher frequency, the result is automatically forward- filled (if ``position=='START'``) or backward-filled (if ``position='END'``) The function accepts the same input parameters and has the same signature as the standard :func:`scikits.timeseries.convert` function. Please refer to the docstring of this latter for more information. See Also -------- convert Original function """ if not isinstance(series, TimeSeries): raise TypeError("The input should be a valid TimeSeries object!") result = ts.convert(series, freq, func=func, position=position, *args, **kwargs) if freq > series._dates.freq: _series = result._series if position.upper()[0] == 'S': _series[:] = ts.lib.forward_fill(_series) else: _series[:] = ts.lib.backward_fill(_series) return result
def weighted(self,*args): R = None (weights,[data,series]) = args logger.debug("Params are (w=%s,d=%s,s=%s)" % (weights,data,series)) if check_type(weights,str,unicode) and check_type(data,str,unicode) and check_type(series,str,unicode): logger.debug("Params are (w=%s,d=%s,s=%s)" % (weights,data,series)) Ws = weights if ',' in series: Ss = series.split(',') else: raise ValueError,"La stringa di definizione delle serie è errata" if ',' in weights: Ws = weights.split(',') else: if '$$' not in weights: raise ValueError,"La stringa di definizione dei pesi non permette la generazione di un elenco" Ws = [ weights.replace('$$',S) for S in Ss ] if ',' in data: Ds = data.split(',') else: if '$$' not in data: raise ValueError,"La stringa di definizione dei dati non permette la generazione di un elenco" Ds = [ data.replace('$$',S) for S in Ss ] if len(Ws)==len(Ds): W = 0.0 S = 0.0 for d,w in zip(Ds,Ws): wb=IS.has_key(w) db=IS.has_key(d) if wb and db: Wn = IS[w].data[0] Dn = IS[d].data try: # if isinstance(Dn,ts.TimeSeries): # Dn = np.nan_to_num(Dn) # if isinstance(W,ts.TimeSeries): # W = np.nan_to_num(W) # if isinstance(W,ts.TimeSeries): # ts.align_series(W,Dn) if isinstance(Dn,ts.TimeSeries): Dn = ts.time_series(Dn,copy=True) if isinstance(W,ts.TimeSeries): Dn = ts.convert(Dn,W.freq) ts.fill_missing_dates(Dn,dates=W.dates,fill_value=np.nan) Dn = ts.adjust_endpoints(Dn, start_date=W.start_date, end_date=W.end_date, copy=True) # _ts = np.ma.masked_invalid(Dn) # Dn = _ts.filled(0.0) # if isinstance(W,ts.TimeSeries): # print "GOò" # _report(W,Dn*Wn) W += Dn*Wn # if isinstance(W,ts.TimeSeries): # print "=" # _report(W) except Exception, exc: # if isinstance(W,ts.TimeSeries): # _report(W) # print "DN" # _report(Dn) # print w,d,"W",type(W),W,W.shape,"D",type(Dn),Dn,Dn.shape,"Wn",type(Wn),Wn raise logger.error('Non posso comporre %s * %s | %s', d,w) ValueError, "%s * %s" % (d,w) S += Wn else: if not wb: logger.error(u'la serie %s non è presente nell\'IS. ATTENZIONE i risultati dell\'aggregazione possono non essere sono attendibili',w) if not db: logger.error(u'la serie %s non è presente nell\'IS. ATTENZIONE i risultati dell\'aggregazione possono non essere sono attendibili',d) try: R = ets.Timeseries(data=W / S,name="WEIGHTED(\"%s\",\"%s\")" % (','.join(Ws),','.join(Ds))) except ZeroDivisionError, exc: logger.warn('ZeroDivisionError') R = None
if namo == 'LNS12032195': stitle = sera['title'].split(',')[0] else: stitle = sera['title'].replace(r'&','and') newstr = str(i1+7)+r" & "+stitle+r" & "+namo+r" & "+r"St. Louis Fed"+r" & "+ sera['units']+r" & "+stringo+r" & "+str(stlouislist[i1])+r"\\"+"\n" tmpstr = tmpstr + newstr freqf = resuf['seriess']['series']['frequency_short'] print "Done..." valo = [x['value'] for x in resu['observations']['observation']] valo2 = numpy.zeros(len(valo)) for i1,elem in enumerate(valo): if elem == '.': valo2[i1] = float(valo2[i1-1]) else: valo2[i1] = float(elem) valo = numpy.array(valo2) dato = [x['date'] for x in resu['observations']['observation']] if freqf == 'D': tso = ts.time_series(valo,dates=dato,freq='D') tso2 = ts.convert(tso,'M',func=numpy.ma.mean) elif freqf == 'W': tso = ts.time_series(valo,dates=dato,freq='D') tso2 = ts.convert(tso,'M',func=numpy.ma.mean) elif freqf == 'M': tso = ts.time_series(valo,dates=dato,freq='M') tso2 = copy.deepcopy(tso) elif freqf == 'Q': tso = ts.time_series(valo,dates=dato,freq='Q') tso2 = ts.convert(tso,'M') tso2 = interp_masked1d(tso2) valor = tso2['1985-01-01':'2011-09-01'] dator = valor.dates valor = valor.data valor = numpy.reshape(valor,(len(valor),1)) matd = numpy.hstack((matd,valor))
) + r" & " + stitle + r" & " + namo + r" & " + r"St. Louis Fed" + r" & " + sera[ 'units'] + r" & " + stringo + r" & " + str( stlouislist[i1]) + r"\\" + "\n" tmpstr = tmpstr + newstr freqf = resuf['seriess']['series']['frequency_short'] print "Done..." valo = [x['value'] for x in resu['observations']['observation']] valo2 = numpy.zeros(len(valo)) for i1, elem in enumerate(valo): if elem == '.': valo2[i1] = float(valo2[i1 - 1]) else: valo2[i1] = float(elem) valo = numpy.array(valo2) dato = [x['date'] for x in resu['observations']['observation']] if freqf == 'D': tso = ts.time_series(valo, dates=dato, freq='D') tso2 = ts.convert(tso, 'M', func=numpy.ma.mean) elif freqf == 'W': tso = ts.time_series(valo, dates=dato, freq='D') tso2 = ts.convert(tso, 'M', func=numpy.ma.mean) elif freqf == 'M': tso = ts.time_series(valo, dates=dato, freq='M') tso2 = copy.deepcopy(tso) elif freqf == 'Q': tso = ts.time_series(valo, dates=dato, freq='Q') tso2 = ts.convert(tso, 'M') tso2 = interp_masked1d(tso2) valor = tso2['1985-01-01':'2011-09-01'] dator = valor.dates valor = valor.data valor = numpy.reshape(valor, (len(valor), 1)) matd = numpy.hstack((matd, valor))