Fix weekly return aggregation on pandas >= 2.0 (DatetimeIndex.week removed)#533
Open
nyxst4ck wants to merge 1 commit into
Open
Fix weekly return aggregation on pandas >= 2.0 (DatetimeIndex.week removed)#533nyxst4ck wants to merge 1 commit into
nyxst4ck wants to merge 1 commit into
Conversation
`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
marked this pull request as ready for review
July 14, 2026 02:09
Author
|
Marked ready for review. I do not see any checks reported on this branch yet. |
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.
Problem
utils.aggregate_returns()usesDatetimeIndex.weekfor 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: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 toaggregate_returns:best,worst,win_rate,avg_return,avg_win,avg_loss,consecutive_winsandconsecutive_lossesare all affected.Fix
Replace
index.weekwithindex.isocalendar().week. The old.week/.weekofyearattributes were aliases for the ISO week number, which is exactly whatisocalendar().weekreturns, so this restores the original behavior without changing results. Pairing withindex.year(as before) is preserved for the end-of-week grouping.Tests
Added regression tests in
tests/test_utils.pycovering weekly aggregation via bothaggregate_returns("W"and"week") and the stats public API (best/worstwithaggregate="W"), including a value check against an explicitisocalendar-based compounding reference. The new tests fail onmain(AttributeError) and pass with this change; the full existing suite continues to pass.