Skip to content

fix(analyser): 兼容自定义数据源返回 DataFrame 的 benchmark 行情 - #1029

Open
shuxue6662-a11y wants to merge 1 commit into
ricequant:developfrom
shuxue6662-a11y:codex/fix-benchmark-dataframe-access
Open

fix(analyser): 兼容自定义数据源返回 DataFrame 的 benchmark 行情#1029
shuxue6662-a11y wants to merge 1 commit into
ricequant:developfrom
shuxue6662-a11y:codex/fix-benchmark-dataframe-access

Conversation

@shuxue6662-a11y

Copy link
Copy Markdown

Fixes #917

问题

本地自定义数据源(如 xtdata)的 history_bars 返回 pandas DataFrame,而内置 bundle 数据源返回 numpy structured array。_get_one_benchmark_daily_returnsbars[1]['datetime'] 的行访问方式对 DataFrame 会触发 KeyError: 1(DataFrame 的 bars[1] 是取列),导致配置 benchmark 后回测直接报错。

修改

  • history_bars 结果统一转换为 pandas DataFrame,统一使用 bars['datetime'].iloc[...] 访问行,兼容 numpy structured array 与 pandas DataFrame 两类数据源
  • 切片改为 bars.iloc[left:],避免依赖 DataFrame 的标签索引
  • 新增单元测试,覆盖 DataFrame 与 numpy records 两种数据源

验证

  • pytest tests/unittest/test_mod/test_sys_analyser.py:2 个测试全部通过
  • pytest tests/unittest:除缺少本地 bundle 数据及环境 i18n 差异导致的既有失败外,无新增失败

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant