Skip to content

Resolve duplicate "datetime" level across remaining qlib call sites#2217

Open
genisis0x wants to merge 1 commit into
microsoft:mainfrom
genisis0x:fix/datetime-level-positional-broader
Open

Resolve duplicate "datetime" level across remaining qlib call sites#2217
genisis0x wants to merge 1 commit into
microsoft:mainfrom
genisis0x:fix/datetime-level-positional-broader

Conversation

@genisis0x
Copy link
Copy Markdown

Summary

Follow-up hardening on top of #2216 (which fixed PortAnaRecord for #1909).

Auditing the codebase for the same name-based index.get_level_values(\"datetime\") pattern surfaced several additional sites that would crash with the same ValueError: 'datetime' occurs multiple times on the same MultiIndex shape:

  • qlib/utils/__init__.pysplit_pred
  • qlib/contrib/evaluate.py — long/short returns loop in backtest_analysis
  • qlib/workflow/online/update.pyPredUpdater.__init__ and _replace_range
  • qlib/workflow/online/manager.pyOnlineManager.prepare_signals and routine
  • qlib/model/ens/ensemble.pyRollingEnsemble.__call__
  • qlib/data/dataset/__init__.pyDatasetH.setup_data

All call sites switch to positional level resolution:

dt_level = index.names.index(\"datetime\")
index.get_level_values(dt_level)

This matches the resolution used in #2216.

Test plan

  • New tests/misc/test_datetime_level_lookup_helpers.py builds the same duplicate-datetime MultiIndex used in the Fix PortAnaRecord crash on duplicate "datetime" level (#1909) #2216 regression test and exercises split_pred, asserting:
    • the precondition that the name-based lookup raises
    • split_pred runs and returns a complete partition (left + right = input)
  • No behavior change on single-datetime indexes (positional lookup of the first datetime level is equivalent when the name is unique)

Refs #1909

Follow-up hardening on top of the microsoft#1909 PortAnaRecord fix. Several other
sites still call `index.get_level_values("datetime")` and would crash
with `ValueError: 'datetime' occurs multiple times` on the same
MultiIndex shape:

- qlib/utils/__init__.py::split_pred
- qlib/contrib/evaluate.py::backtest_analysis (long/short returns loop)
- qlib/workflow/online/update.py::PredUpdater.__init__ and _replace_range
- qlib/workflow/online/manager.py::OnlineManager.prepare_signals / routine
- qlib/model/ens/ensemble.py::RollingEnsemble.__call__
- qlib/data/dataset/__init__.py::DatasetH.setup_data

All switch to positional level resolution via
`index.names.index("datetime")`. Added a regression test in
`tests/misc/test_datetime_level_lookup_helpers.py` covering split_pred.

Refs microsoft#1909
@genisis0x
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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