Skip to content

fix(records): respect group_by in MappedArray.describe on pandas 3 - #910

Merged
polakowo merged 2 commits into
polakowo:masterfrom
wavebyrd:fix/mapped-describe-group-by
Nov 8, 2026
Merged

polakowo merged 2 commits into
polakowo:masterfrom
wavebyrd:fix/mapped-describe-group-by

Conversation

@wavebyrd

@wavebyrd wavebyrd commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

On pandas 3, MappedArray.describe takes a shortcut through self.to_pd().describe() when the result is not grouped. That call drops group_by, so describe(group_by=False) on a grouped mapped array (e.g. trades of a grouped portfolio) expands with the default grouping: it either returns one column per group or fails with "Multiple values are pointing to the same position". It also fails with "Must pass idx_arr" for mapped arrays without an idx_arr. Now the shortcut uses to_pd(ignore_index=True, group_by=group_by), which needs neither unique positions nor idx_arr and gives the same stats. Added a group_by=False assertion to test_describe; the records tests pass.

import pandas as pd
import vectorbt as vbt

close = pd.DataFrame({"a": [1.0, 2.0, 3.0, 4.0], "b": [4.0, 3.0, 2.0, 1.0]})
size = pd.DataFrame({"a": [1.0, -1.0, 0.0, 0.0], "b": [0.0, 1.0, -1.0, 0.0]})
pf = vbt.Portfolio.from_orders(close, size, group_by=True, cash_sharing=True, freq="1d")

pf.trades.pnl.describe(group_by=False)
# before: a single Series named "group" (count 2, mean 0.0)
# after:  a DataFrame with columns a and b (pnl 1.0 and -1.0)

@polakowo polakowo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the fix! Passing group_by and ignoring record positions fits describe; all 149 records tests pass. The CI failures are the old timezone fixture fixed in #907.

@polakowo
polakowo merged commit fb2b768 into polakowo:master Nov 8, 2026
25 checks passed
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