尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

3 步搞定 RD-Agent Qlib 股票索引缺失:KeyError 彻底消失的完整修复指南

3 步搞定 RD-Agent Qlib 股票索引缺失:KeyError 彻底消失的完整修复指南 3 步搞定 RD-Agent Qlib 股票索引缺失KeyError 彻底消失的完整修复指南【免费下载链接】RD-AgentResearch and development (RD) is crucial for the enhancement of industrial productivity, especially in the AI era, where the core aspects of RD are mainly focused on data and models. We are committed to automating these high-value generic RD processes through RD-Agent, which lets AI drive>项目地址: https://gitcode.com/GitHub_Trending/rd/RD-AgentRD-Agent 因子循环跑到第 3 轮终端突然甩出一行 KeyError: SH600519——Qlib 数据股票索引缺失的经典现场。这个坑我踩了一下午今天把修复路线讲清楚。Qlib 数据索引为什么会断第一个断点在数据生成。数据生成脚本用 D.features() 一次性拉取全市场行情返回的是 MultiIndexpandas 的多层行索引datetime、instrument 两层。底层 qlib 数据源只要有一点缺口——某只票的缓存文件丢了、代码没登记进 instrument 列表——这批股票代码就直接不返回。不报错只是安静地少几行。第二个断点在因子合并。factor_runner.py 的合并分支把 SOTA 因子和新因子按列方向 concat再 dropna()。两张表的股票池只要对不齐缺失的一侧全是 NaNdropna 把整行吃掉。就像两张花名册做 join一张少了三个人那三行直接没了。⚠️ 这两处最阴的地方都不报错。第一处静悄悄第二处只是让回测样本变少。项目其实在 developer/utils.py 的 process_factor_data 里已经做过一次拦截——缺 instrument 层的表会被直接丢弃日志里只留一行警告。但丢弃不等于修复。# 数据生成数据源缺票代码就不返回不报错 data D.features(instruments, fields, freqday) # 因子合并股票池对不齐 → 缺失侧填 NaN → 被 dropna 吃掉 combined pd.concat([sota, new_factors], axis1).dropna()先止血、再补齐、后加固3 步修复止血3 行 assert 堵住空索引改动落在数据生成脚本 factor_data_template/generate.py。D.features() 拿到数据后加三行校验。挂了当场挂问题暴露在数据层别拖到回测轮次# 堵住空索引让问题在数据层第一时间炸出来 assert data.index.nlevels 2, 索引必须是 datetimeinstrument 两层 assert instrument in data.index.names, 缺少股票代码层 assert data.index.get_level_values(instrument).nunique() 0, 股票列表为空补齐用基础股票池一键回填缺失代码索引不空但缺个别代码时在 factor_runner.py 的合并分支处理。Qlib 因子回测索引对齐的核心就一句话先把两边索引拉齐。从 daily_pv.h5 读基础股票池把两边 reindex 到它的索引上再 concat。之后 dropna 删掉的是真缺的数据而不是对不齐的股票池# 以基础股票池为准先对齐两边索引再合并 base pd.read_hdf(daily_pv.h5) # 基础股票池索引齐全 new_factors new_factors.reindex(base.index) # 缺失代码回填 NaN结构统一 sota sota.reindex(base.index) # SOTA 侧同样处理加固把校验写进数据生成入口不再靠人肉人肉跑的校验迟早会忘。experiment/utils.py 里的 generate_data_folder_from_qlib 原本只断言生成了 h5 文件顺手把索引完整也断言进去。之后无论是新机器部署、CI 跑批还是缓存重建RD-Agent 数据完整性校验都会自动跑一遍不用人盯。跑通后 5 分钟自查✅ 修完之后对着下面 5 条打勾。全过索引问题就算闭环daily_pv.h5 的索引是两层 MultiIndex层名依次为 [datetime, instrument]instrument 唯一值数量与 qlib 数据源股票池数量一致无缺票合并后因子表列数 SOTA 列数 新因子列数无列被吞日志里没有 index misalignment 警告没抛 FactorEmptyError重跑一次基线实验回测指标与上一轮一致 建议每次 pull 主分支后重跑一遍 generate.py索引结构可能随数据源升级微调。 RD-Agent KeyError 修复与因子循环的配置细节可参考 docs/scens/quant_agent_fin.rst。【免费下载链接】RD-AgentResearch and development (RD) is crucial for the enhancement of industrial productivity, especially in the AI era, where the core aspects of RD are mainly focused on data and models. We are committed to automating these high-value generic RD processes through RD-Agent, which lets AI drive>项目地址: https://gitcode.com/GitHub_Trending/rd/RD-Agent创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表