Skip to content

DOC: Is sort preserved inside groupby? #9651

Closed
@nickeubank

Description

@nickeubank

Empirically, it seems to be the case that if one sorts a dataframe then takes a groupby, that sort is preserved within each groupby group. For example, it seems to be the case that my_func below always gets a df sorted by age. But I can't find any documentation on whether this is guaranteed, so I'm currently re-sorting data in my_func to be safe.

df.sort(columns = ['users', 'age'], inplace = True)
df.groupby('users').apply(my_func)

Does anyone know if this is a guaranteed behavior, and if so, might it be worth adding to docs?

This is particularly relevant for the groupby().nth() command, since if sort isn't preserved, it's not clear what it grabs!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions