Koalas: Does koalas groupby preserve the order appeared in the original dataframe like pandas?

Created on 21 Aug 2020  路  3Comments  路  Source: databricks/koalas

I'm implementing a function which groups dataframe and sort in each group with a key in descending order and then pick top k records from each group. There is no sorting function which works on groupby-dataframe so I applied sort_values first and then groupby. The function would look like this.

top_k_selected_df = (
    df.sort_values("sortby", ascending=False)
     .groupby(by=["key1", "key2"], as_index=False)
     .head(k)
)

pandas doc says that groupby preserves the order like this below so the function above works for pandas dataframe.

Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame:

https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#groupby-sorting

But I didn't find such a mention in koalas doc so I wondered if I can expect that koalas also gurantees the order like pandas or it breaks and I need to implement this with pyspark instead.

question

Most helpful comment

Note that, for groupby-head case, the row order within each group will be preserved. The row order of the result dataframe will not be guaranteed.
For the above case, groupby.head will take k rows by sortby desc order in each group and returns a dataframe in unknown order.

All 3 comments

Nope, in general the order is not guaranteed. It needs an explicit sorting for now.

Thank you for confirming it! As the feature can't be implemented with koalas, I'll implement this with vanilla pyspark then.

Note that, for groupby-head case, the row order within each group will be preserved. The row order of the result dataframe will not be guaranteed.
For the above case, groupby.head will take k rows by sortby desc order in each group and returns a dataframe in unknown order.

Was this page helpful?
0 / 5 - 0 ratings