Closed
Description
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!