def Compare2Industry(company): ''' 辅助函数:获取配置文件中所指代公司的上一年年报数据,用于同行业对比(用户不必关心) 输入:行业对比 输出:DataFrame形式的结果(最后一行是输入的平均水平) ''' result = [] index_id = [] print('get compare report data...') for individual in company: try: if Config.data_src == 'SQL': a = GetItemInfo.GetSingleItem(individual,datetime.now().year - 1) elif Config.data_src == 'CSV': a = GetItemInfo.GetSingleLocalItem(individual,datetime.now().year - 1) else: print('compare failure. bad parameter') return result.append(a) index_id.append(individual) except: print('drop ', individual,'\'s report') pass result = pd.DataFrame(result,index = index_id) result.loc['avarage'] = result.apply(lambda x: x.sum()/len(index_id)) # result.to_csv('compare_industry.csv') return result
def Compare2Themself(target_id, start_year = 2010): ''' 辅助函数:获取target_id从2010年开始直到去年的财务数据,形成DataFrame并输出(用户不必关心) 输入:开始时间(可选) 输出:DataFrame形式的结果 ''' result = [] index_id = [] print('get self report data...') for year in range(start_year, datetime.now().year): try: if Config.data_src == 'SQL': a = GetItemInfo.GetSingleItem(target_id,year) elif Config.data_src == 'CSV': a = GetItemInfo.GetSingleLocalItem(target_id,year) else: print('compare failure. bad parameter') return result.append(a) index_id.append(year) except Exception as e: print (e) print('pass ', str(year)) pass result = pd.DataFrame(result,index = index_id) return result
def Compare2Themself(target_id, start_year=2010): ''' 辅助函数:获取target_id从2010年开始直到去年的财务数据,形成DataFrame并输出(用户不必关心) 输入:开始时间(可选) 输出:DataFrame形式的结果 ''' result = [] index_id = [] print('get self report data...') for year in range(start_year, datetime.now().year): try: a = GetItemInfo.GetSingleItem(parameter, target_id, year) result.append(a) index_id.append(year) except: pass result = pd.DataFrame(result, index=index_id) return result
tag = head[30] #毛利率 opt = data[tag] result[tag] = opt tag = head[31] #营收增长率 opt = data[tag] result[tag] = opt tag = head[32] #除非净利润增长率 opt = data[tag] result[tag] = opt return result ############################################################################### if __name__ == '__main__': # 1. 初始化配置 t_cur, parameter, company = Config.M1809_config() #获取配置信息 GetItemInfo.SetCur(t_cur) #配置cur,否则无法联上数据库 a = Compare2Themself(company[0]) a.to_csv('./output/compare_self.csv', encoding='gbk') b1 = Compare2Industry(company) b1.to_csv('./output/compare_industry.csv', encoding='gbk') b = data_normalize(b1) b.to_csv('test.csv', encoding='gbk') Analyse(a, b) #b要做标准化处理 # PlotAnalyse(a)