缺失数据精准补充

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

confined.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值