def job_8(): try: print("I'm working......银行间同业拆放利率") # Shibor拆放利率 shibor_data = ts.shibor_data() data = pd.DataFrame(shibor_data) data.to_sql('shibor_data',engine,index=True,if_exists='replace') print("银行间同业拆放利率......done") # 银行报价数据 shibor_quote_data = ts.shibor_quote_data() data = pd.DataFrame(shibor_quote_data) data.to_sql('shibor_quote_data',engine,index=True,if_exists='replace') print("银行报价数据......done") # Shibor均值数据 shibor_ma_data = ts.shibor_ma_data() data = pd.DataFrame(shibor_ma_data) data.to_sql('shibor_ma_data',engine,index=True,if_exists='replace') print("Shibor均值数据......done") # 贷款基础利率(LPR) lpr_data = ts.lpr_data() data = pd.DataFrame(lpr_data) data.to_sql('lpr_data',engine,index=True,if_exists='replace') print("贷款基础利率......done") # LPR均值数据 lpr_ma_data = ts.lpr_ma_data() data = pd.DataFrame(lpr_ma_data) data.to_sql('lpr_ma_data',engine,index=True,if_exists='replace') print("LPR均值数据......done") except Exception as e: print(e)
def get_lpr(year=None): """贷款基础利率(LPR)(Shanghai Interbank Offered Rate,简称Shibor) Args: year: 年份(YYYY),默认为当前年份 """ if not year: year = util.get_year() logger.info('Begin get LPR, year is: %s.' % year) try: data_df = ts.lpr_data(year) except Exception as e: logger.exception('Error get LPR. year is: %s.' % year) return None else: data_df['date'] = data_df['date'].astype(str) data_dicts = [] if data_df is None or data_df.empty: logger.warn('Empty get LPR. year is: %s.' % year) else: data_df['date'] = data_df['date'].astype(str) data_dicts = [{ 'date': row[0], 'Y1': row[1], 'year': year } for row in data_df.values] logger.info('Success get LPR. year is: %s.' % year) return data_dicts
def getLPRDate(self): file_name = str(self.date)+'_lpr'+'.csv' path = self.index + self.index_lpr_data + file_name data = ts.lpr_data(year = self.date) data.sort('date', ascending=False).head(10) data.to_csv(path, encoding='utf-8') print(file_name)
def __call__(self, conns): self.base = Base() self.financial_data = conns['financial_data'] year = self.base.gettoday()[:4] #Shibor拆放利率 shibor_data = ts.shibor_data(year) print(shibor_data) self.base.batchwri(shibor_data, 'shibor_data', self.financial_data) #银行报价数据 shibor_quote_date = ts.shibor_quote_data(year) self.base.batchwri(shibor_quote_date, 'shibor_quote_data', self.financial_data) #Shibor均值数据 shibor_ma_data = ts.shibor_ma_data(year) self.base.batchwri(shibor_ma_data, 'shibor_ma_data', self.financial_data) #贷款基础利率(LPR) lpr_data = ts.lpr_data(year) self.base.batchwri(lpr_data, 'lpr_data', self.financial_data) #LPR均值数据 lpr_ma_data = ts.lpr_ma_data(year) self.base.batchwri(lpr_ma_data, 'lpr_ma_data', self.financial_data)
def add_lpr_page(canvas_para, year_start='2013', year_end=str(datetime.datetime.now().year + 1)): """ 函数功能:增加贷款利率页 :param canvas_para: :return: """ c = canvas_para date_list = pd.date_range(start=year_start, end=year_end, freq='12M') year_list = [str(x)[0:4] for x in date_list] df_Lpr_list = [] for year in year_list: lpr_this = ts.lpr_data(year) df_Lpr_list.append(lpr_this) df_Lpr = pd.concat(df_Lpr_list, axis=0).sort_values( by='date', ascending=True).drop_duplicates(subset='1Y', keep='first') Y1 = extract_point_from_df_date_x(df_origin=df_Lpr, date_col='date', y_col='1Y', timeAxis='datetime') lpr_drawing = gen_lp_drawing([tuple(Y1)], data_note=['1年贷款基础利率'], time_axis='day', height=letter[1] * 0.3, y_min_zero=True) renderPDF.draw(drawing=lpr_drawing, canvas=c, x=10, y=letter[1] * 0.6) # 画均值贷款利率 # df_Lpr_ma_list = [] # for year in year_list: # lpr_ma_this = ts.lpr_ma_data(year) # df_Lpr_ma_list.append(lpr_ma_this) # # df_Lpr_ma = pd.concat(df_Lpr_ma_list, axis=0).sort_values(by='date', ascending=True)\ # .drop_duplicates(subset=['1Y_5', '1Y_10', '1Y_20'], keep='first')\ # .apply(lambda x:x.replace('---',nan), axis=1) # # Y1_5 = ExtractPointFromDf_DateX(df_origin=df_Lpr_ma, date_col='date', y_col='1Y_5', timeAxis='datetime') # Y1_10 = ExtractPointFromDf_DateX(df_origin=df_Lpr_ma, date_col='date', y_col='1Y_10', timeAxis='datetime') # Y1_20 = ExtractPointFromDf_DateX(df_origin=df_Lpr_ma, date_col='date', y_col='1Y_20', timeAxis='datetime') # # lpr_ma_drawing = genLPDrawing([tuple(Y1_5),tuple(Y1_10),tuple(Y1_20)], # data_note=['1年贷款基础利率-M5','1年贷款基础利率-M10','1年贷款基础利率-M20'], # timeAxis='day', # height=letter[1] * 0.3) # # renderPDF.draw(drawing=lpr_ma_drawing, canvas=c, x=10, y=letter[1] * 0.2) c.showPage() return c
def setLprData(self, year=None, number=1, isSave=False, tableName=CALLLOANS_LPR_DATA): yearList = getYearList(year, number) for yearItem in yearList: df = ts.lpr_data(yearItem) if isSave is True: df.to_sql(tableName, self.engine_sql, if_exists='append') return df
def lpr(engine, year, sdate=None): tbl = "lpr" tsl.log(tbl + " start...") try: df = ts.lpr_data(year) if sdate is not None: df = df[df.date >= sdate] df.to_sql(tbl, engine, if_exists='append') tsl.log(tbl + " done") except BaseException, e: print e tsl.log(tbl + " error")
def get_shibor_lpr_ma_data(year=None): """ LPR均值数据 获取贷款基础利率均值数据,目前只提供2013年以来的数据。 """ df = ts.lpr_data(year) print(df) if df is not None: res = df.to_sql(shibor_lpr_ma_data, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取贷款基础利率均值数据: 年:{0} {1}'.format(year, msg) + '\n') else: print('获取贷款基础利率均值数据: 年:{0} {1}'.format(year, 'None') + '\n')
def get_shibor_data((shibor_type,yearlist)): if shibor_type == 'data': DTS=[] for year in yearlist: preDTS = ts.shibor_data(year) DTS.append(preDTS) DTS=pd.concat(DTS) DTS.to_csv('D:\\ts\\shibor\\shibor_%s.csv'%shibor_type,encoding='gbk') elif shibor_type == 'quote_data': DTS = [] for year in yearlist: preDTS = ts.shibor_quote_data(year) DTS.append(preDTS) DTS = pd.concat(DTS) DTS.to_csv('D:\\ts\\shibor\\shibor_%s.csv' % shibor_type, encoding='gbk') elif shibor_type == 'ma_data': DTS = [] for year in yearlist: preDTS = ts.shibor_ma_data(year) DTS.append(preDTS) DTS = pd.concat(DTS) DTS.to_csv('D:\\ts\\shibor\\shibor_%s.csv' % shibor_type, encoding='gbk') elif shibor_type == 'lpr_data': DTS = [] for year in yearlist: preDTS = ts.lpr_data(year) DTS.append(preDTS) DTS = pd.concat(DTS) DTS.to_csv('D:\\ts\\shibor\\shibor_%s.csv' % shibor_type, encoding='gbk') elif shibor_type == 'lpr_ma_data': DTS = [] for year in yearlist: preDTS = ts.lpr_ma_data(year) DTS.append(preDTS) DTS = pd.concat(DTS) DTS.to_csv('D:\\ts\\shibor\\shibor_%s.csv' % shibor_type, encoding='gbk')
def shibor_quote_data(year=""): jsonFile = os.path.join(config.listsRootPath, "shibor_quote_data.json") data = ts.lpr_data() if str(type(data)) == "<class 'pandas.core.frame.DataFrame'>": data.sort('date', ascending=False) writeFile(jsonFile, data, 'records', False)
#三大需求对GDP贡献 gdp_for = ts.get_gdp_for() #三大产业对GDP拉动 gdp_pull = ts.get_gdp_pull() #三大产业贡献率 gdp_contrib = ts.get_gdp_contrib() cpi = ts.get_cpi() ppi = ts.get_ppi() df = ts.shibor_data() #取当前年份的数据 #df = ts.shibor_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10) df = ts.shibor_quote_data() #取当前年份的数据 #df = ts.shibor_quote_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10) #shibo均值 df = ts.shibor_ma_data() #取当前年份的数据 #df = ts.shibor_ma_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10) #贷款基础利率 lpr = ts.lpr_data() #取当前年份的数据 #df = ts.lpr_data(2014) #取2014年的数据
# -*- coding: utf-8 -*- import tushare as ts ''' 获取贷款基础利率(LPR)数据,目前只提供2013年以来的数据。 参数说明: year:年份(YYYY),默认为当前年份 返回值说明: date:日期 1Y:1年贷款基础利率 ''' df = ts.lpr_data() ##df.sort('date', ascending=False).head(10) print(df) '''获取贷款基础利率均值数据,目前只提供2013年以来的数据。 参数说明: year:年份(YYYY),默认为当前年份 返回值说明: date:日期 1Y_5:5日均值 1Y_10:10日均值 1Y_20:20日均值'''
def fetchByYear(self, mongo, func): years = range(self.begin_year, self.end_year_notinclude) for year in years: print(year) print(type(year)) # year = int(year) #print (type(year)) if (func == 'shibor_data'): if (year < 2006): print("[Wrong]没有" + str(year) + "年数据") continue else: print("有" + str(year) + "年数据") #df = ts.shibor_data() # 取当前年份的数据 print("shibor") print(type(ts.shibor_data(year))) df = ts.shibor_data(year) #取2014年的数据 #df.sort('date', ascending=False) elif (func == 'shibor_quote_data'): if (year < 2006): print("[Wrong]没有" + str(year) + "年数据") continue else: print("有" + str(year) + "年数据") #df = ts.shibor_quote_data() # 取当前年份的数据 df = ts.shibor_quote_data(year) #取2014年的数据 #df.sort('date', ascending=False) elif (func == 'shibor_ma_data'): if (year < 2006): print("[Wrong]没有" + str(year) + "年数据") continue else: print("有" + str(year) + "年数据") #df = ts.shibor_ma_data() # 取当前年份的数据 df = ts.shibor_ma_data(year) #取2014年的数据 print("4444444444444") print(df) #df.sort('date', ascending=False) elif (func == 'lpr_data'): if (year < 2013): print("[Wrong]没有" + str(year) + "年数据") continue else: print("有" + str(year) + "年数据") #df = ts.lpr_data() # 取当前年份的数据 df = ts.lpr_data(year) #取2014年的数据 #df.sort('date', ascending=False) # df = fd.lpr_data(self.year) elif (func == 'lpr_ma_data'): if (year < 2013): print("[Wrong]没有" + str(year) + "年数据") continue else: print("有" + str(year) + "年数据") # df = ts.lpr_ma_data() # 取当前年份的数据 df = ts.lpr_ma_data(year) #取2014年的数据 #df.sort('date', ascending=False) else: print("error" + func) df = {"err": True} year = str(year) print("###################") print(df) print(func) tmpJson = json.loads(df.to_json(orient='records')) # tmpJson = json.dumps(df) print("_________") print(tmpJson) print(type(tmpJson)) import time for i in range(len(tmpJson)): tmpJson[i][u'year'] = int(year) d = time.localtime(tmpJson[i][u'date'] / 1000) tmpJson[i][u'date'] = time.strftime('%Y-%m-%d', d) coll = mongo.shibor[func] coll2 = mongo.shibor[func + '_' + str(year)] coll2.insert(tmpJson) coll.insert(tmpJson)
import pymongo import json import pandas as pd # ts.get_cash_flow : get specialed Code Cash_flow in the History . # 600547:山东黄金 # 600362:江西铜业 # 600312:平高电气 # 600499:科达洁能 # 603993:洛阳钼业 db = "Shibor" coll = "Shibor_LPR" year = 2016 conn = pymongo.MongoClient('127.0.0.1', port=27017) df = ts.lpr_data(year) # index data columns(X columns) dicIndex = json.loads(df.to_json(orient='split')) for i, ind in enumerate(dicIndex['index']): # Note: pandas has some transformation between date,timestampe and string. d = pd.to_datetime(dicIndex['data'][i][0], unit='ms').strftime('%Y-%m-%d') jsonstr = { '_id': d, 'year': year, dicIndex['columns'][0]: d, dicIndex['columns'][1]: dicIndex['data'][i][1], } try: conn[db][coll].insert(jsonstr) except: pass