Skip to content

DOC: Getting started example on using groupby().mean() alongside pivot throws error #55599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
ragibson opened this issue Oct 19, 2023 · 0 comments · Fixed by #55600
Closed
1 task done

DOC: Getting started example on using groupby().mean() alongside pivot throws error #55599

ragibson opened this issue Oct 19, 2023 · 0 comments · Fixed by #55600
Labels
Docs Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ragibson
Copy link
Contributor

ragibson commented Oct 19, 2023

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/getting_started/intro_tutorials/07_reshape_table_layout.html

Documentation problem

If you run this line discussing the connection between pivot tables and groupby(),

air_quality.groupby(["parameter", "location"]).mean()

you actually get TypeErrors related to string conversions and the aggregation function failing.

Suggested fix for documentation

Restricting to numeric values only shows the intended result that matches the pivot table. I.e.,

print(air_quality.pivot_table(
    values="value",
    index="location",
    columns="parameter",
    aggfunc="mean",
    margins=True
))

print(air_quality.groupby(["parameter", "location"]).mean(numeric_only=True))

shows

parameter                 no2       pm25        All
location                                           
BETR801             26.950920  23.169492  24.982353
FR04014             29.374284        NaN  29.374284
London Westminster  29.740050  13.443568  21.491708
All                 29.430316  14.386849  24.222743
                                  value
parameter location                     
no2       BETR801             26.950920
          FR04014             29.374284
          London Westminster  29.740050
pm25      BETR801             23.169492
          London Westminster  13.443568
@ragibson ragibson added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2023
@rhshadrach rhshadrach added Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2023
@rhshadrach rhshadrach modified the milestone: 2.2 Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants