def add_demands_for_gdp_page(canvas_para): """ 函数功能:三大需求对GDP的贡献 :param canvas_para: :return: """ c = canvas_para gdp_for = ts.get_gdp_for() end_for = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='end_for', timeAxis='year') asset_for = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='asset_for', timeAxis='year') goods_for = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='goods_for', timeAxis='year') gdp_for_drawing = gen_lp_drawing([tuple(end_for), tuple(asset_for), tuple(goods_for)], ['最终消费支出贡献率', '资本形成总额贡献率', '货物和服务净出口贡献率'], time_axis='year') renderPDF.draw(drawing=gdp_for_drawing, canvas=c, x=10, y=letter[1] * 0.6) for_rate = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='for_rate', timeAxis='year') asset_rate = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='asset_rate', timeAxis='year') goods_rate = extract_point_from_df_date_x(df_origin=gdp_for, date_col='year', y_col='goods_rate', timeAxis='year') gdp_for_drawing = gen_lp_drawing([tuple(for_rate), tuple(asset_rate), tuple(goods_rate)], ['最终消费支出拉动(百分点)', '资本形成总额拉动(百分点)', '货物和服务净出口拉动(百分点)'], time_axis='year') renderPDF.draw(drawing=gdp_for_drawing, canvas=c, x=10, y=letter[1] * 0.2) c.showPage() return c
def get_gdp_for(): try: df = ts.get_gdp_for() engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8') df.to_sql('gdp_for', engine, if_exists='append') print "message" except Exception, e: e.message
def get_gdp_for_info(): df = ts.get_gdp_for() if df is not None: res = df.to_sql(microE_gdp_for, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取三大需求对GDP贡献: ' + msg + '\n') else: print('获取三大需求对GDP贡献: ' + 'None' + '\n')
def stat_all(tmp_datetime): # 存款利率 data = ts.get_deposit_rate() common.insert_db(data, "ts_deposit_rate", False, "`date`,`deposit_type`") # 贷款利率 data = ts.get_loan_rate() common.insert_db(data, "ts_loan_rate", False, "`date`,`loan_type`") # 存款准备金率 data = ts.get_rrr() common.insert_db(data, "ts_rrr", False, "`date`") # 货币供应量 data = ts.get_money_supply() common.insert_db(data, "ts_money_supply", False, "`month`") # 货币供应量(年底余额) data = ts.get_money_supply_bal() common.insert_db(data, "ts_money_supply_bal", False, "`year`") # 国内生产总值(年度) data = ts.get_gdp_year() common.insert_db(data, "ts_gdp_year", False, "`year`") # 国内生产总值(季度) data = ts.get_gdp_quarter() common.insert_db(data, "ts_get_gdp_quarter", False, "`quarter`") # 三大需求对GDP贡献 data = ts.get_gdp_for() common.insert_db(data, "ts_gdp_for", False, "`year`") # 三大产业对GDP拉动 data = ts.get_gdp_pull() common.insert_db(data, "ts_gdp_pull", False, "`year`") # 三大产业贡献率 data = ts.get_gdp_contrib() common.insert_db(data, "ts_gdp_contrib", False, "`year`") # 居民消费价格指数 data = ts.get_cpi() common.insert_db(data, "ts_cpi", False, "`month`") # 工业品出厂价格指数 data = ts.get_ppi() common.insert_db(data, "ts_ppi", False, "`month`") #############################基本面数据 http://tushare.org/fundamental.html # 股票列表 data = ts.get_stock_basics() print(data.index) common.insert_db(data, "ts_stock_basics", True, "`code`")
def return_gdp_for(self): ''' 三大需求对GDP贡献 ''' df = ts.get_gdp_for() detail = {} for col in df.columns: lt = df[col].values.tolist() lt.reverse() for idx in xrange(0, len(lt)): if math.isnan(lt[idx]): lt[idx] = None detail[col] = lt self.reply(detail=detail)
def get_macro(): Macro={} Macro['Depo']=ts.get_deposit_rate() Macro['Loan']=ts.get_loan_rate() Macro['RRR']=ts.get_rrr() Macro['MoneySupply']=ts.get_money_supply() Macro['MoneyBalance']=ts.get_money_supply_bal() Macro['GDPYOY']=ts.get_gdp_year() Macro['GDPQOQ']=ts.get_gdp_quarter() Macro['GDPFOR']=ts.get_gdp_for() Macro['GDPPULL']=ts.get_gdp_pull() Macro['GDPCON']=ts.get_gdp_contrib() Macro['CPI']=ts.get_cpi() Macro['PPI']=ts.get_ppi() Macro['SHIBO']=ts.shibor_data() return Macro
def get_gdp_three_demands(): """三大需求对GDP贡献""" logger.info('Begin get GdpThreeDemands.') try: data_df = ts.get_gdp_for() except Exception as e: logger.exception('Error get GdpThreeDemands.') return None else: data_dicts = [] if data_df.empty: logger.warn('Empty get GdpThreeDemands.') else: data_dicts = [{'year': row[0], 'end_for': row[1], 'for_rate': row[2], 'asset_for': row[3], 'asset_rate': row[4], 'goods_for': row[5], 'goods_rate': row[6]} for row in data_df.values] logger.info('Success get GdpThreeDemands.') return data_dicts
def __call__(self, conns): self.base = Base() self.financial_data = conns['financial_data'] '''存款利率''' deposit_rate = ts.get_deposit_rate() self.base.batchwri(deposit_rate, 'deposit_rate', self.financial_data) '''贷款利率''' loan_rate = ts.get_loan_rate() self.base.batchwri(loan_rate, 'loan_rate', self.financial_data) '''存款准备金率''' rrr = ts.get_rrr() self.base.batchwri(rrr, 'RatioOfDeposit', self.financial_data) '''货币供应量''' money_supply = ts.get_money_supply() self.base.batchwri(money_supply, 'money_supply', self.financial_data) '''货币供应量(年底余额)''' money_supply_bal = ts.get_money_supply_bal() self.base.batchwri(money_supply_bal, 'money_supply_bal', self.financial_data) '''国内生产总值(年度)''' gdp_year = ts.get_gdp_year() self.base.batchwri(gdp_year, 'gdp_year', self.financial_data) '''国内生产总值(季度)''' gdp_quarter = ts.get_gdp_quarter() self.base.batchwri(gdp_quarter, 'gdp_quarter', self.financial_data) '''三大需求对GDP贡献''' gdp_for = ts.get_gdp_for() self.base.batchwri(gdp_for, 'gdp_for', self.financial_data) '''三大产业对GDP拉动''' gdp_pull = ts.get_gdp_pull() self.base.batchwri(gdp_pull, 'gdp_pull', self.financial_data) '''三大产业贡献率''' gdp_contrib = ts.get_gdp_contrib() self.base.batchwri(gdp_contrib, 'gdp_contrib', self.financial_data) '''居民消费价格指数''' cpi = ts.get_cpi() self.base.batchwri(cpi, 'cpi', self.financial_data) '''工业品出场价格指数''' ppi = ts.get_ppi() self.base.batchwri(ppi, 'ppi', self.financial_data)
def getGDPFor(self): file_name = 'gdp_for.csv' path = self.index + self.index_gdp_for + file_name data = ts.get_gdp_for() data.to_csv(path, encoding='utf-8') print(file_name)
''' ts.get_gdp_quarter() # 三大需求对GDP贡献 ''' year :统计年度 end_for :最终消费支出贡献率(%) for_rate :最终消费支出拉动(百分点) asset_for :资本形成总额贡献率(%) asset_rate:资本形成总额拉动(百分点) goods_for :货物和服务净出口贡献率(%) goods_rate :货物和服务净出口拉动(百分点) ''' ts.get_gdp_for() # 三大产业对GDP拉动 ''' year :统计年度 gdp_yoy :国内生产总值同比增长(%) pi :第一产业拉动率(%) si :第二产业拉动率(%) industry:其中工业拉动(%) ti :第三产业拉动率(%) ''' ts.get_gdp_pull() # 三大产业贡献率 '''
rrr=ts.get_rrr() rrr.to_csv('D:\\ts\\macro\\rrr.csv', encoding='gbk') money_supply=ts.get_money_supply() money_supply.to_csv('D:\\ts\\macro\\money_supply.csv', encoding='gbk') money_supply_bal=ts.get_money_supply_bal() money_supply_bal.to_csv('D:\\ts\\macro\\money_supply_bal.csv', encoding='gbk') gdp_year=ts.get_gdp_year() gdp_year.to_csv('D:\\ts\\macro\\gdp_year.csv', encoding='gbk') gdp_quater=ts.get_gdp_quarter() gdp_quater.to_csv('D:\\ts\\macro\\gdp_quater.csv', encoding='gbk') gdp_for=ts.get_gdp_for() gdp_for.to_csv('D:\\ts\\macro\\gdp_for.csv', encoding='gbk') gdp_pull=ts.get_gdp_pull() gdp_pull.to_csv('D:\\ts\\macro\\gdp_pull.csv', encoding='gbk') gdp_contrib=ts.get_gdp_contrib() gdp_contrib.to_csv('D:\\ts\\macro\\gdp_contrib.csv', encoding='gbk') cpi=ts.get_cpi() cpi.to_csv('D:\\ts\\macro\\cpi.csv', encoding='gbk') ppi=ts.get_ppi() ppi.to_csv('D:\\ts\\macro\\ppi.csv', encoding='gbk')
def macro_type(macros_type): if macros_type == 'deposit_rate': deposit_rate = ts.get_deposit_rate() if deposit_rate is not None: deposit_rate.to_sql('macros_deposit_rate', engine, flavor='mysql', if_exists='replace') elif macros_type == 'loan_rate': loan_rate = ts.get_loan_rate() if loan_rate is not None: loan_rate.to_sql('macros_loan_rate', engine, flavor='mysql', if_exists='replace') elif macros_type == 'rrr': rrr = ts.get_rrr() if rrr is not None: rrr.to_sql('macros_rrr', engine, flavor='mysql', if_exists='replace') elif macros_type == 'money_supply': money_supply = ts.get_money_supply() if money_supply is not None: money_supply.to_sql('macros_money_supply', engine, flavor='mysql', if_exists='replace') elif macros_type == 'money_supply_bal': money_supply_bal = ts.get_money_supply_bal() if money_supply_bal is not None: money_supply_bal.to_sql('macros_money_supply_bal', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_year': gdp_year = ts.get_gdp_year() if gdp_year is not None: gdp_year.to_sql('macros_gdp_year', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_quater': gdp_quater = ts.get_gdp_quarter() if gdp_quater is not None: gdp_quater.to_sql('macros_gdp_quater', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_for': gdp_for = ts.get_gdp_for() if gdp_for is not None: gdp_for.to_sql('macros_gdp_for', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_pull': gdp_pull = ts.get_gdp_pull() if gdp_pull is not None: gdp_pull.to_sql('macros_gdp_pull', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_contrib': gdp_contrib = ts.get_gdp_contrib() if gdp_contrib is not None: gdp_contrib.to_sql('macros_gdp_contrib', engine, flavor='mysql', if_exists='replace') elif macros_type == 'cpi': cpi = ts.get_cpi() if cpi is not None: cpi.to_sql('macros_cpi', engine, flavor='mysql', if_exists='replace') elif macros_type == 'ppi': ppi = ts.get_ppi() if ppi is not None: ppi.to_sql('macros_ppi', engine, flavor='mysql', if_exists='replace')
ms = ts.get_money_supply_bal() print(ms) # 国内生产总值(年度) gy = ts.get_gdp_year() print(gy) # 国内生产总值(季度) gq = ts.get_gdp_quarter() print(gq) # 三大需求对GDP贡献 gf = ts.get_gdp_for() print(gf) # 三大产业对GDP拉动 gp = ts.get_gdp_pull() print(gp) # 三大产业贡献率 gc = ts.get_gdp_contrib() print(gc) # 居民消费价格指数 cpi = ts.get_cpi()
@contact: [email protected] @version: 1.0 @Description: """ import numpy as np import pandas as pd import tushare as ts import matplotlib.pyplot as plt np.set_printoptions(threshold=2000, linewidth=1000) # default 1000 default 75 pd.set_option('display.width', 1000) # default is 80 if __name__ == "__main__": td = ts.get_gdp_for() """ year :统计年度 end_for :最终消费支出贡献率(%) for_rate :最终消费支出拉动(百分点) asset_for :资本形成总额贡献率(%) asset_rate:资本形成总额拉动(百分点) goods_for :货物和服务净出口贡献率(%) goods_rate :货物和服务净出口拉动(百分点) """ td = td.set_index("year") td = td.iloc[::-1, :] print(td) td_pull = td[["for_rate", "asset_rate", "goods_rate"]]
def init(engine, session): tbl = "macro_deposit" tsl.log(tbl + " start...") df = ts.get_deposit_rate() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_loan" tsl.log(tbl + " start...") df = ts.get_loan_rate() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_rrr" tsl.log(tbl + " start...") df = ts.get_rrr() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_money_supply" tsl.log(tbl + " start...") df = ts.get_money_supply() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_money_supply_year" tsl.log(tbl + " start...") df = ts.get_money_supply_bal() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_year" tsl.log(tbl + " start...") df = ts.get_gdp_year() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_quarter" tsl.log(tbl + " start...") df = ts.get_gdp_quarter() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_for" tsl.log(tbl + " start...") df = ts.get_gdp_for() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_pull" tsl.log(tbl + " start...") df = ts.get_gdp_pull() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_contrib" tsl.log(tbl + " start...") df = ts.get_gdp_contrib() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_cpi" tsl.log(tbl + " start...") df = ts.get_cpi() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_ppi" tsl.log(tbl + " start...") df = ts.get_ppi() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "gold_and_foreign_reserves" tsl.log(tbl + " start...") df = ts.get_gold_and_foreign_reserves() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done")
def capture_stock_data(): capture_date = datetime.datetime.now().strftime("%Y%m%d") save_dir = "/home/dandelion/stock_data/" + capture_date if not os.path.exists(save_dir): os.mkdir(save_dir) print("The save directory is created successfully!\n", save_dir) print("The save directory is already exist!\n", save_dir) # ======================Daily Command================================================================ # get the boxoffcie data of the last day and save as csvfile named as the capture command ts.day_boxoffice().to_csv( save_dir + "/" + capture_date + "_day_boxoffice.csv", header=True, sep=",", index=False, ) print("day_boxoffice data capture completed!") # get the cinema data of the last day and save as csvfile named as the capture command ts.day_cinema().to_csv( save_dir + "/" + capture_date + "_day_cinema.csv", header=True, sep=",", index=False, ) print("day_cinema data capture completed!") ts.month_boxoffice().to_csv( save_dir + "/" + capture_date + "_month_boxoffice.csv", header=True, sep=",", index=False, ) print("month_boxoffice data capture completed!") ts.realtime_boxoffice().to_csv( save_dir + "/" + capture_date + "_realtime_boxoffice.csv", header=True, sep=",", index=False, ) print("realtime_boxoffice data capture completed!") # get the stock data index of the last day and save as csvfile named as the capture command ts.get_index().to_csv( save_dir + "/" + capture_date + "_get_index.csv", header=True, sep=",", index=False, ) print("get_index data capture completed!") # get the history cpi data and save as csvfile named as the capture command ts.get_cpi().to_csv( save_dir + "/" + capture_date + "_get_cpi.csv", header=True, sep=",", index=False, ) print("get_cpi data capture completed!") # get the history gdp data by month and save as csvfile named as the capture command ts.get_gdp_year().to_csv( save_dir + "/" + capture_date + "_get_gdp_year.csv", header=True, sep=",", index=False, ) print("get_gdp_year data capture completed!") # get today all stock data and save as csvfile named as the capture command # ts.get_today_all().to_csv(save_dir+'/'+capture_date+'_get_today_all.csv',header=True,sep=',',index=False) # get detail information of the top brokers today and save as csvfile named as the capture command ts.broker_tops().to_csv( save_dir + "/" + capture_date + "_broker_tops.csv", header=True, sep=",", index=False, ) print("broker_tops data capture completed!") # get detail information of the top brokers today and save as csvfile named as the capture command ts.cap_tops().to_csv( save_dir + "/" + capture_date + "_cap_tops.csv", header=True, sep=",", index=False, ) print("cap_tops data capture completed!") ts.get_area_classified().to_csv( save_dir + "/" + capture_date + "_get_area_classified.csv", header=True, sep=",", index=False, ) print("get_area_classified data capture completed!") # ts.get_balance_sheet(code='').to_csv(save_dir+'/'+capture_date+'_get_balance_sheet.csv',header=True,sep=',',index=False) # print('get_balance_sheet data capture completed!') # ts.get_cash_flow(code='').to_csv(save_dir+'/'+capture_date+'_get_cash_flow.csv',header=True,sep=',',index=False) # print('get_cash_flow data capture completed!') ts.get_day_all().to_csv( save_dir + "/" + capture_date + "_get_day_all.csv", header=True, sep=",", index=False, ) print("get_day_all data capture completed!") ts.get_cashflow_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_cashflow_data.csv", header=True, sep=",", index=False, ) print("get_cashflow_data data capture completed!") ts.get_concept_classified().to_csv( save_dir + "/" + capture_date + "_get_concept_classified.csv", header=True, sep=",", index=False, ) print("get_concept_classified data capture completed!") ts.get_debtpaying_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_debtpaying_data.csv", header=True, sep=",", index=False, ) print("get_debtpaying_data data capture completed!") ts.get_deposit_rate().to_csv( save_dir + "/" + capture_date + "_get_deposit_rate.csv", header=True, sep=",", index=False, ) print("get_deposit_rate data capture completed!") ts.get_gdp_contrib().to_csv( save_dir + "/" + capture_date + "_get_gdp_contrib.csv", header=True, sep=",", index=False, ) ts.get_gdp_for().to_csv( save_dir + "/" + capture_date + "_get_gdp_for.csv", header=True, sep=",", index=False, ) ts.get_gdp_pull().to_csv( save_dir + "/" + capture_date + "_get_gdp_pull.csv", header=True, sep=",", index=False, ) ts.get_gdp_quarter().to_csv( save_dir + "/" + capture_date + "_get_gdp_quarter.csv", header=True, sep=",", index=False, ) print("get_gdp_ data capture completed!") # ts.get_gdp_year().to_csv(save_dir+'/'+capture_date+'_get_gdp_year.csv',header=True,sep=',',index=False) ts.get_gem_classified().to_csv( save_dir + "/" + capture_date + "_get_gem_classified.csv", header=True, sep=",", index=False, ) ts.get_gold_and_foreign_reserves().to_csv( save_dir + "/" + capture_date + "_get_gold_and_foreign_reserves.csv", header=True, sep=",", index=False, ) ts.get_growth_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_growth_data.csv", header=True, sep=",", index=False, ) ts.get_industry_classified().to_csv( save_dir + "/" + capture_date + "_get_industry_classified.csv", header=True, sep=",", index=False, ) ts.get_hs300s().to_csv( save_dir + "/" + capture_date + "_get_hs300s.csv", header=True, sep=",", index=False, ) ts.get_sz50s().to_csv( save_dir + "/" + capture_date + "_get_sz50s.csv", header=True, sep=",", index=False, ) ts.get_zz500s().to_csv( save_dir + "/" + capture_date + "_get_zz500s.csv", header=True, sep=",", index=False, ) ts.get_operation_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_operation_data.csv", header=True, sep=",", index=False, ) ts.get_stock_basics().to_csv( save_dir + "/" + capture_date + "_get_stock_basics.csv", header=True, sep=",", index=False, ) ts.get_report_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_report_data.csv", header=True, sep=",", index=False, ) ts.inst_detail().to_csv( save_dir + "/" + capture_date + "_inst_detail.csv", header=True, sep=",", index=False, ) ts.inst_tops().to_csv( save_dir + "/" + capture_date + "_inst_tops.csv", header=True, sep=",", index=False, ) print("inst_tops data capture completed!") ts.new_stocks().to_csv( save_dir + "/" + capture_date + "_new_stocks.csv", header=True, sep=",", index=False, ) print("new_stocks data capture completed!") ts.top_list().to_csv( save_dir + "/" + capture_date + "_top_list.csv", header=True, sep=",", index=False, ) print("top_list data capture completed!")
def getgdpfordb(): gdpfor = ts.get_gdp_for() gdpfor.to_sql('gdpfor_data',ENGINE,if_exists='append')
def setGdpFor(self, isSave=False, tableName=MACROECONOMIC_GDP_FOR): df = ts.get_gdp_for() if isSave is True: df.to_sql(tableName, self.engine_sql, if_exists='append') return df
def job_5(): try: print("I'm working......宏观经济数据") # 存款利率 deposit_rate = ts.get_deposit_rate() data = pd.DataFrame(deposit_rate) data.to_sql('deposit_rate',engine,index=True,if_exists='replace') print("存款利率......done") # 贷款利率 loan_rate = ts.get_loan_rate() data = pd.DataFrame(loan_rate) data.to_sql('loan_rate',engine,index=True,if_exists='replace') print("贷款利率......done") # 存款准备金率 rrr = ts.get_rrr() data = pd.DataFrame(rrr) data.to_sql('rrr',engine,index=True,if_exists='replace') print("存款准备金率......done") # 货币供应量 money_supply = ts.get_money_supply() data = pd.DataFrame(money_supply) data.to_sql('money_supply',engine,index=True,if_exists='replace') print("货币供应量......done") # 货币供应量(年底余额) money_supply_bal = ts.get_money_supply_bal() data = pd.DataFrame(money_supply_bal) data.to_sql('money_supply_bal',engine,index=True,if_exists='replace') print("货币供应量(年底余额)......done") # 国内生产总值(年度) gdp_year = ts.get_gdp_year() data = pd.DataFrame(gdp_year) data.to_sql('gdp_year',engine,index=True,if_exists='replace') print("国内生产总值(年度)......done") # 国内生产总值(季度) gdp_quarter = ts.get_gdp_quarter() data = pd.DataFrame(gdp_quarter) data.to_sql('gdp_quarter',engine,index=True,if_exists='replace') print("国内生产总值(季度)......done") # 三大需求对GDP贡献 gdp_for = ts.get_gdp_for() data = pd.DataFrame(gdp_for) data.to_sql('gdp_for',engine,index=True,if_exists='replace') print("三大需求对GDP贡献......done") # 三大产业对GDP拉动 gdp_pull = ts.get_gdp_pull() data = pd.DataFrame(gdp_pull) data.to_sql('gdp_pull',engine,index=True,if_exists='replace') print("三大产业对GDP拉动......done") # 三大产业贡献率 gdp_contrib = ts.get_gdp_contrib() data = pd.DataFrame(gdp_contrib) data.to_sql('gdp_contrib',engine,index=True,if_exists='replace') print("三大产业贡献率......done") # 居民消费价格指数 cpi = ts.get_cpi() data = pd.DataFrame(cpi) data.to_sql('cpi',engine,index=True,if_exists='replace') print("居民消费价格指数......done") # 工业品出厂价格指数 ppi = ts.get_ppi() data = pd.DataFrame(ppi) data.to_sql('ppi',engine,index=True,if_exists='replace') print("工业品出厂价格指数......done") except Exception as e: print(e)
__author__ = 'Shawn Li' import tushare as ts from sklearn.neural_network import MLPRegressor from sklearn.preprocessing import MinMaxScaler import pandas as pd from pandas import Series, DataFrame import numpy as np import matplotlib.pyplot as plt import seaborn as sns money_supply = ts.get_money_supply_bal() gdp_year = ts.get_gdp_year() gdp_for = ts.get_gdp_for() length = len(money_supply) def float_array(array): float_arr = np.array(array) float_arr = float_arr.astype(np.float64) return float_arr m2 = float_array(money_supply['m2'])[0:length] m1 = float_array(money_supply['m1'])[0:length] m0 = float_array(money_supply['m0'])[0:length] cd = float_array(money_supply['cd'])[0:length] gdp = float_array(gdp_year['gdp'])[0:length] pi = float_array(gdp_year['pi'])[0:length]
def load_macro_economy(): # 下载存款利率 try: rs = ts.get_deposit_rate() pd.DataFrame.to_sql(rs, "deposit_rate", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载存款利率ok") except: print("下载存款利率出错") # 下载贷款利率 try: rs = ts.get_loan_rate() pd.DataFrame.to_sql(rs, "loan_rate", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载贷款利率ok") except: print("下载贷款利率出错") # 下载存款准备金率 try: rs = ts.get_rrr() pd.DataFrame.to_sql(rs, "rrr", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载存款准备金率ok") except: print("下载存款准备金率出错") # 下载货币供应量 try: rs = ts.get_money_supply() pd.DataFrame.to_sql(rs, "money_supply", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载货币供应量ok") except: print("下载货币供应量出错") # 下载货币供应量(年底余额) try: rs = ts.get_money_supply_bal() pd.DataFrame.to_sql( rs, "money_supply_bal", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True ) print("下载货币供应量(年底余额)ok") except: print("下载货币供应量(年底余额)出错") # 下载国内生产总值(年度) try: rs = ts.get_gdp_year() pd.DataFrame.to_sql(rs, "gdp_year", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载国内生产总值(年度)ok") except: print("下载国内生产总值(年度)出错") # 下载国内生产总值(季度) try: rs = ts.get_gdp_quarter() pd.DataFrame.to_sql(rs, "gdp_quarter", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载国内生产总值(季度)ok") except: print("下载国内生产总值(季度)出错") # 下载三大需求对GDP贡献 try: rs = ts.get_gdp_for() pd.DataFrame.to_sql(rs, "gdp_for", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大需求对GDP贡献ok") except: print("下载三大需求对GDP贡献出错") # 下载三大产业对GDP拉动 try: rs = ts.get_gdp_pull() pd.DataFrame.to_sql(rs, "gdp_pull", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大产业对GDP拉动ok") except: print("下载三大产业对GDP拉动出错") # 下载三大产业贡献率 try: rs = ts.get_gdp_contrib() pd.DataFrame.to_sql(rs, "gdp_contrib", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大产业贡献率ok") except: print("下载三大产业贡献率出错") # 下载居民消费价格指数 try: rs = ts.get_cpi() pd.DataFrame.to_sql(rs, "gdp_cpi", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载居民消费价格指数ok") except: print("下载居民消费价格指数出错") # 下载工业品出厂价格指数 try: rs = ts.get_ppi() pd.DataFrame.to_sql(rs, "gdp_ppi", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载工业品出厂价格指数ok") except: print("下载工业品出厂价格指数出错")