Skip to content

Fixed aggregation issue in grouping#187

Open
mwael-labs wants to merge 3 commits into
guipsamora:masterfrom
mwael-labs:master
Open

Fixed aggregation issue in grouping#187
mwael-labs wants to merge 3 commits into
guipsamora:masterfrom
mwael-labs:master

Conversation

@mwael-labs

Copy link
Copy Markdown

This PR updates the Step 4 solution in the Pandas exercises 03_Grouping -> Regiment.
The original code worked in older Pandas versions (pre 1.0) but now raises a TypeError in modern Pandas (>= 1.5, 2.0+) because of stricter handling of non‑numeric columns during aggregation.

Problem

regiment[regiment['regiment'] == 'Nighthawks'].groupby('regiment').mean()

Old behaviour ignored non-numeric columns.
New behaviour attempts to aggregate all columns including non-numeric ones, which caused
TypeError: agg function failed [how->mean,dtype->object]

Fix

the correct code should explicitly select preTestScore column.
regiment[regiment['regiment'] == 'Nighthawks'] .groupby('regiment')['preTestScore'] .mean()

Output

regiment Nighthawks 15.25 Name: preTestScore, dtype: float64

Compatibility Note

This fix reflects changes introduced in Pandas after 2020:

Older notebooks may still run without error on legacy Pandas.

Modern environments require explicit column selection or numeric_only=True

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