Skip to content

BUG: Fix Backtest() with a non-datetime index on pandas 3 - #1410

Open
David-Wu1119 wants to merge 1 commit into
kernc:masterfrom
David-Wu1119:fix/pandas3-numeric-index
Open

David-Wu1119 wants to merge 1 commit into
kernc:masterfrom
David-Wu1119:fix/pandas3-numeric-index

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Sep 23, 2026 •

Copy link
Copy Markdown

On pandas 3.0, Backtest(data, ...) raises for any index that is neither a DatetimeIndex nor a RangeIndex:

import pandas as pd
from backtesting import Backtest, Strategy
from backtesting.test import GOOG

class Noop(Strategy):
    def init(self): pass
    def next(self): pass

data = GOOG.copy()
data.index = data.index.strftime('%Y-%m-%d')   # or epoch integers
Backtest(data, Noop)
# AttributeError: 'Index' object has no attribute 'is_numeric'

Index.is_numeric() was deprecated in pandas 2.0 and removed in 3.0. setup.py allows pandas >= 0.25, so a fresh install gets pandas 3. The conversion branch below it also passed infer_datetime_format=True, which 3.0 removed too. That raises TypeError, which the surrounding except ValueError doesn't catch.

This PR:

  • replaces data.index.is_numeric() with pd.api.types.is_numeric_dtype(data.index), which exists in every supported pandas version;
  • drops infer_datetime_format=True. It only affected string parsing, and this branch only handles numeric input.

The existing suite uses GOOG's DatetimeIndex, 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:

  • An epoch-nanosecond integer index is still converted to the original DatetimeIndex.
  • A date-string index still gets the "index is not datetime" warning instead of an exception.

On master with pandas 3.0.6 the test fails with the AttributeError above.

Verification:

  • python -m backtesting.test: 82 tests pass with 1 skipped, on both pandas 3.0.6 and pandas 2.2.3.
  • flake8 backtesting is clean.
  • mypy backtesting reports the same 8 errors as master.

馃 Generated with Claude Code

`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>
Copilot AI lite review requested due to automatic review settings September 23, 2026 17:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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.

2 participants