缺失数据精准补充

index_table表存放自己需要的指数信息,但是可能因为服务器报错或其他原因,会漏采一些数据。
这时候如果去可以设置一个trade_date表,trade_date表里存放交易日日期。

通过该sql就可以得到需要补充的日期:(因为数据源获取数据有数量限制,所以精准获取,可以节约账号流量)

#可以得到2019年12月1日到今天(2023-05-15)的所有数据缺失日期。
SELECT trade_date as statistic_date FROM trade_date where trade_date>=‘2019-12-01’ and trade_date<=‘2023-05-15’ and trade_date not in (SELECT statistic_date FROM index_table WHERE index_id=‘需要补充的id’)

	df = pd.read_excel(r'指数.xlsx')
    index = df['if_id'].tolist()
    #if-id是获取数据源时需要的id,id是最后入数据库的id。
    #excel是对id做一个映射
    dict_s = df.set_index('if_id')['id'].to_dict()

    for z in index:
        db_id=dict_s[z]
        sql_date="SELECT trade_date  as statistic_date   FROM trade_date where   trade_date>='2019-12-01' and trade_date<='2023-05-15'  and trade_date not in (SELECT statistic_date  FROM index_table   WHERE  index_id='{}')".format(db_id)
        df_date=pd.read_sql(sql_date,index_database())['statistic_date']

        print(db_id)
        print(len(df_date))
        for my_date in df_date:
            try:
                start=str(my_date)
                end=str(my_date)
                df_bond = get_data(z,start, end)
                print(df_bond)
                
                df_bond['index_id'] = df_bond['index_id'].apply(lambda x : dict_s[x])
                to_sql('index_table', index_database(), df_bond, type='update')
                print(df_bond)
            except Exception as e:
                print(e)
                continue

猜你喜欢

转载自blog.csdn.net/qq_44821149/article/details/130686145