class PlotlyAPI(object): def __init__(self): self.influx = InfluxDB() def _create_scatter(self,expression,mode): try: axis_separate = expression.index(':') axis_label = expression[:axis_separate] expression = expression[axis_separate+1:] except: axis_label = 'y1' try: name_separate = expression.index(';') legend = expression[name_separate+1:] except: name_separate = len(expression) legend = expression result = self.influx.interpret(expression[:name_separate]) x = result.index.to_pydatetime() y = result['value'].get_values() return Scatter(x=x,y=y,mode=mode,name=legend) def _create_ts(self,exp_list,title,mode='lines'): scatters = [] for exp in exp_list: scatters.append(self._create_scatter(exp,mode)) data = Data(scatters) py.plot(data, filename=title)
processed_list = [ [ 'wage_lag_15m', 'mlag(wage_us_yoy,15)' ], [ 'zillow_median_sale_price_15m', 'mlag(zillow_median_sale_price_yoy,15)' ], ] df = DataFrameClient('localhost',8086,'root','root') if({'name':'Econ'} not in df.get_list_database()): df.create_database('Econ') df.switch_database('Econ') #add all items in interested list start = timeit.default_timer() for item in interest_list: if item[0] in df.get_list_series(): df.delete_series(item[0]) results = influx_Fred.interpret(item[1]) results = results.replace(to_replace='NaN',value='.') results = DataFrame({'value':results['value']}) df.write_points({item[0]:results}) print item print 'total time in seconds: %.2f' % (timeit.default_timer() - start) #add all items in processed list start = timeit.default_timer() influx_Econ = InfluxDB(db_name='Econ') for item in processed_list: if item[0] in df.get_list_series(): df.delete_series(item[0]) results = influx_Econ.interpret(item[1]) results = results.replace(to_replace='NaN',value='.') results = DataFrame({'value':results['value']})
''' Created on Jul 26, 2015 @author: shaunz ''' from QueryData.InfluxQuery import InfluxDB from pandas.core.config import option_context influx = InfluxDB() one = influx.query('cn_gdp_cum') two = influx.interpret('anticum(cn_gdp_cum,Q)*100') with option_context('display.max_rows', 9999, 'display.max_columns', 3): print one print two ''' new_series = {} for each in one.iterrows(): if each[0].month != 1: prev_date = each[0] + DateOffset(months=-1) prev_date = prev_date.to_period('M').to_timestamp('M') monthly = each[1]['value']-one.loc[prev_date] new_series[each[0]] = monthly['value'] else: new_series[each[0]] = each[1]['value'] Series(new_series) '''
''' Created on Jul 18, 2015 @author: shaunz ''' from QueryData.InfluxQuery import InfluxDB from pandas.stats.moments import rolling_corr from pandas.tools.merge import concat from pandas.core.common import isnull from pandas.core.config import option_context db = InfluxDB(db_name='Econ') series_1 = db.interpret('cpi_us_owners_equiv_yoy') series_2 = db.interpret('avg(zillow_median_sale_price_yoy,M)') series_1.rename(columns={'value':'value_1'}, inplace=True) series_2.rename(columns={'value':'value_2'}, inplace=True) periods = 60 analysis_type = 'snapshot' if(analysis_type=='snapshot'): for i in range(-36, 37): # go back i periods shifted_1 = series_1.shift(i) combined = concat([shifted_1['value_1'],series_2['value_2']],axis=1,join='outer') combined = combined.loc[isnull(combined['value_1']) != True] combined = combined.loc[isnull(combined['value_2']) != True] results = rolling_corr(combined['value_1'],combined['value_2'],window=periods) print '%d,%.4f' % (i, results[-1:].values) elif(analysis_type=='history'): shifted_1 = series_1.shift(0) combined = concat([shifted_1['value_1'],series_2['value_2']],axis=1,join='outer') combined = combined.loc[isnull(combined['value_1']) != True]