Skip to content

Fix weekly return aggregation on pandas >= 2.0 (DatetimeIndex.week removed)#533

Open
nyxst4ck wants to merge 1 commit into
ranaroussi:mainfrom
nyxst4ck:fix-weekly-aggregation-pandas2
Open

Fix weekly return aggregation on pandas >= 2.0 (DatetimeIndex.week removed)#533
nyxst4ck wants to merge 1 commit into
ranaroussi:mainfrom
nyxst4ck:fix-weekly-aggregation-pandas2

Conversation

@nyxst4ck

Copy link
Copy Markdown

Problem

utils.aggregate_returns() uses DatetimeIndex.week for the weekly grouping paths ("week", "eow", and "W"). That accessor was deprecated in pandas 1.1 and removed in pandas 2.0, so any weekly aggregation raises:

AttributeError: 'DatetimeIndex' object has no attribute 'week'

Since the project targets pandas >= 1.5 (and Python 3.10–3.13), this breaks for the large majority of users who are on pandas 2.x / 3.x.

The break also surfaces through the public stats API, which forwards aggregate="W" down to aggregate_returns:

import numpy as np, pandas as pd
import quantstats as qs

r = pd.Series(np.random.randn(60) * 0.01,
              index=pd.date_range("2020-01-01", periods=60, freq="D"))

qs.stats.best(r, aggregate="W")   # AttributeError on pandas >= 2.0

best, worst, win_rate, avg_return, avg_win, avg_loss, consecutive_wins and consecutive_losses are all affected.

Fix

Replace index.week with index.isocalendar().week. The old .week/.weekofyear attributes were aliases for the ISO week number, which is exactly what isocalendar().week returns, so this restores the original behavior without changing results. Pairing with index.year (as before) is preserved for the end-of-week grouping.

Tests

Added regression tests in tests/test_utils.py covering weekly aggregation via both aggregate_returns ("W" and "week") and the stats public API (best/worst with aggregate="W"), including a value check against an explicit isocalendar-based compounding reference. The new tests fail on main (AttributeError) and pass with this change; the full existing suite continues to pass.

`aggregate_returns` used `DatetimeIndex.week` for the "week"/"eow"/"W"
grouping paths. That accessor was deprecated in pandas 1.1 and removed
in pandas 2.0, so any weekly aggregation raises
`AttributeError: 'DatetimeIndex' object has no attribute 'week'` on
modern pandas.

This affects the public API too: `best`, `worst`, `win_rate`,
`avg_return`, `avg_win`, `avg_loss`, `consecutive_wins` and
`consecutive_losses` all crash when called with `aggregate="W"`.

Replace `index.week` with `index.isocalendar().week`, which returns the
same ISO week number that `.week` was an alias for, restoring the
original behavior on pandas 2.x / 3.x. Add regression tests for weekly
aggregation via both `aggregate_returns` and the stats public API.
@nyxst4ck
nyxst4ck marked this pull request as ready for review July 14, 2026 02:09
@nyxst4ck

Copy link
Copy Markdown
Author

Marked ready for review. I do not see any checks reported on this branch yet.

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