def get_lpr_ma(year=None): """上海银行间同业拆放利率(Shanghai Interbank Offered Rate,简称Shibor) Args: year: 年份(YYYY),默认为当前年份 """ if not year: year = util.get_year() logger.info('Begin get LprMA, year is: %s.' % year) try: data_df = ts.lpr_ma_data(year) except Exception as e: logger.exception('Error get LprMA. 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 LprMA. year is: %s.' % year) else: data_df['date'] = data_df['date'].astype(str) data_dicts = [{ 'date': row[0], 'Y1_5': row[1], 'Y1_10': row[2], 'Y1_20': row[3], 'year': year } for row in data_df.values] logger.info('Success get LprMA. year is: %s.' % year) return data_dicts
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 getLPRMaDate(self): file_name = str(self.date)+'_lpr_ma'+'.csv' path = self.index + self.index_lpr_ma_data + file_name data = ts.lpr_ma_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 setLprMaData(self, year=None, number=1, isSave=False, tableName=CALLLOANS_LPR_MA_DATA): yearList = getYearList(year, number) for yearItem in yearList: df = ts.lpr_ma_data(yearItem) if isSave is True: df.to_sql(tableName, self.engine_sql, if_exists='append') return df
def lpr_ma(engine, year, sdate=None): tbl = "lpr_ma" tsl.log(tbl + " start...") try: df = ts.lpr_ma_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_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')
''' 获取贷款基础利率(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日均值''' data = ts.lpr_ma_data() #取当前年份的数据 print(data)
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 = "LPR_MA_Data" year = 2016 conn = pymongo.MongoClient('127.0.0.1', port=27017) df = ts.lpr_ma_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], dicIndex['columns'][2]: dicIndex['data'][i][2], dicIndex['columns'][3]: dicIndex['data'][i][3] } try: conn[db][coll].insert(jsonstr)
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)