API: User-control of result keys in GroupBy.apply by TomAugspurger · Pull Request #34998 · pandas-dev/pandas (original) (raw)

group_keys does kick in here, when the applied function returns a subset of the dataframe..

In [8]: df = DataFrame({"key": [1, 1, 1, 2, 2, 2, 3, 3, 3], "value": range(9)}) ...: df.groupby("key", group_keys=True).apply(lambda x: x[:2]) ...: ...: ...: Out[8]: key value key 1 0 1 0 1 1 1 2 3 2 3 4 2 4 3 6 3 6 7 3 7

In [9]: df = DataFrame({"key": [1, 1, 1, 2, 2, 2, 3, 3, 3], "value": range(9)}) ...: df.groupby("key", group_keys=False).apply(lambda x: x[:2]) ...: ...: ...: ...: Out[9]: key value 0 1 0 1 1 1 3 2 3 4 2 4 6 3 6 7 3 7

So it seems the intent is similar to what we want. However I don't know about the defaults. group_keys defaults to true. We'll need to see whether that's appropriate for this case.