Skip to content
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

ENH: do not sort resulting columns when sort=False #46994

Merged
merged 2 commits into from
May 19, 2022

Conversation

ShawnZhong
Copy link
Contributor

@ShawnZhong ShawnZhong commented May 11, 2022

Currently, pivot_table sorts the resulting columns even when sort=False. For example, the following code snippet produces the column ["age", "height"] in alphabetical order instead of the order in the original df or passed by values=.

import pandas as pd

df = pd.DataFrame(
    {
        "firstname": ["John", "Michael"],
        "lastname": ["Foo", "Bar"],
        "height": [173, 182],
        "age": [47, 33],
    }
)
result = df.pivot_table(index=["lastname", "firstname"], values=["height", "age"], sort=False)
print(result)
                    age  height
lastname firstname             
Foo      John        47     173
Bar      Michael     33     182

This PR fixes this issue by not sorting the resulting columns when sort=False

FYI, #17041 has some discussion about the order of the columns. PR #40954 added the keyword sort to pivot_table and was merged in v1.3.

@pep8speaks
Copy link

pep8speaks commented May 11, 2022

Hello @ShawnZhong! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2022-05-15 18:21:51 UTC

@ShawnZhong ShawnZhong force-pushed the pivot_sort branch 4 times, most recently from f0d5007 to d351af4 Compare May 11, 2022 10:02
@jreback
Copy link
Contributor

jreback commented May 12, 2022

I think ok to close #17041 with this PR (if you can add a test for this).

also pls add a whatsnew note. put in reshaping bug fixes section for 1.5

@jreback jreback added this to the 1.5 milestone May 12, 2022
@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label May 12, 2022
@ShawnZhong
Copy link
Contributor Author

I have added the whatsnew note and changed the PR description to close the issue.

Is there a specific case that you want me to add a test for?

@jreback jreback merged commit 7c01e13 into pandas-dev:main May 19, 2022
@jreback
Copy link
Contributor

jreback commented May 19, 2022

thanks @ShawnZhong

@ShawnZhong ShawnZhong deleted the pivot_sort branch May 19, 2022 17:34
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pivot_table not displaying values columns in expected order
3 participants