BUG: Fix Backtest() with a non-datetime index on pandas 3 - #1410
Open
David-Wu1119 wants to merge 1 commit into
Open
David-Wu1119 wants to merge 1 commit into
David-Wu1119 wants to merge 1 commit into
Conversation
`Backtest.__init__` checks `data.index.is_numeric()` for every index that is neither a DatetimeIndex nor a RangeIndex. pandas 3.0 removed `Index.is_numeric()`, so `Backtest(data, ...)` raised AttributeError for data indexed by epoch timestamps or by date strings. The conversion call also passed `infer_datetime_format=True`, which pandas 3.0 removed as well (TypeError, not caught by the surrounding `except ValueError`). Use `pd.api.types.is_numeric_dtype()` (available in every supported pandas) and drop `infer_datetime_format`, which has no effect on numeric input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On pandas 3.0,
Backtest(data, ...)raises for any index that is neither aDatetimeIndexnor aRangeIndex:Index.is_numeric()was deprecated in pandas 2.0 and removed in 3.0.setup.pyallowspandas >= 0.25, so a fresh install gets pandas 3. The conversion branch below it also passedinfer_datetime_format=True, which 3.0 removed too. That raisesTypeError, which the surroundingexcept ValueErrordoesn't catch.This PR:
data.index.is_numeric()withpd.api.types.is_numeric_dtype(data.index), which exists in every supported pandas version;infer_datetime_format=True. It only affected string parsing, and this branch only handles numeric input.The existing suite uses
GOOG'sDatetimeIndex, which short-circuits before the removed call. That's why CI on pandas 3 didn't catch this.Test: added
TestRegressions.test_numeric_and_string_index:DatetimeIndex.On
masterwith pandas 3.0.6 the test fails with theAttributeErrorabove.Verification:
python -m backtesting.test: 82 tests pass with 1 skipped, on both pandas 3.0.6 and pandas 2.2.3.flake8 backtestingis clean.mypy backtestingreports the same 8 errors asmaster.馃 Generated with Claude Code