Skip to content

REF: Fix pandas 3.1 deprecations (dayofweek, inplace=) - #1427

Open
raohemdutt wants to merge 1 commit into
kernc:masterfrom
raohemdutt:fix-pandas-3.1-deprecations
Open

raohemdutt wants to merge 1 commit into
kernc:masterfrom
raohemdutt:fix-pandas-3.1-deprecations

Conversation

@raohemdutt

@raohemdutt raohemdutt commented Oct 23, 2026 •

Copy link
Copy Markdown

pandas 3.1 deprecates a few things this package uses, and each one now raises a Pandas4Warning (a DeprecationWarning subclass):

Location Deprecated call Change
_stats.py:125 index.dayofweek (and index.weekday) index.day_of_week
_util.py:342 df.set_index(..., inplace=True) df = df.set_index(...)
_plotting.py:183 trades.drop(..., inplace=True) trades = trades.drop(...)
backtesting.py:1630 heatmap.sort_index(inplace=True) heatmap = heatmap.sort_index()

Why it matters

CI sets PYTHONWARNINGS, and backtesting/test/__main__.py calls warnings.filterwarnings('error'), so these warnings turn into test errors. The scheduled run on master on 2026-10-01 (run 36806693709) failed in the coverage and deps_prereleases jobs with FAILED (errors=81), all on DatetimeIndex.dayofweek is deprecated. Those jobs had pandas 3.1.0rc0. pandas 3.1.0 is now the normal release that pip install picks, so the build jobs should start failing the same way.

Reproduce

macOS, Python 3.14.3, pandas 3.1.0, numpy 2.5.4, bokeh 3.10.1:

pip install -e '.[test]'
TQDM_DISABLE=1 BOKEH_BROWSER=none \
PYTHONWARNINGS=always::FutureWarning,always::DeprecationWarning \
python -m backtesting.test

On master: FAILED (errors=76, skipped=1).

Testing

  • With warnings set to error, and only bokeh's own warning (below) and the macOS spawn RuntimeWarning ignored: master gives FAILED (errors=74), and this branch gives Ran 81 tests ... OK (skipped=1).
  • Normal python -m backtesting.test: 81 tests, OK (skipped=1). The Pandas4Warnings from backtesting.py are gone.
  • flake8 backtesting setup.py passes. mypy gives the same 2 factor_cmap errors as master, which come from bokeh 3.10's type stubs, so this change doesn't add any.

day_of_week was added in pandas 1.1. bokeh >= 3.1 already requires pandas >= 1.2, so this works with every version the project supports.

Notes

  • Even with this patch, CI will still show DataFrame.reset_index inplace warnings, and they come from bokeh's own code (bokeh/models/sources.py:313), not from backtesting.py. That has to be fixed in bokeh.
  • I noticed that python -m backtesting.test run locally (without PYTHONWARNINGS) does not turn warnings into errors. unittest.main() sets its own 'default' filter when sys.warnoptions is empty, which overrides the filterwarnings('error') in __main__.py. So these warnings only showed up as failures in CI. A short note about this in CONTRIBUTING.md (or passing warnings=False to unittest.main() so the filters already set in __main__.py stay in effect) would let contributors see the same failures locally. I left that out of this PR to keep it small.

馃 Generated with Claude Code

pandas 3.1 emits Pandas4Warning for DatetimeIndex.dayofweek and for
inplace= on DataFrame.set_index/drop and Series.sort_index (PDEP-8).
With CI's PYTHONWARNINGS set, backtesting/test/__main__.py turns these
into errors, which is why the scheduled CI run of 2026-10-01 failed
(coverage and deps_prereleases jobs, "FAILED (errors=81)").

- _stats.py: index.dayofweek -> index.day_of_week (pandas >= 1.1)
- _util.py: df.set_index(..., inplace=True) -> df = df.set_index(...)
- _plotting.py: trades.drop(..., inplace=True) -> reassignment
- backtesting.py: heatmap.sort_index(inplace=True) -> reassignment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

1 participant