I've noticed that there are several uses of pd.DataFrame.groupby
followed by an apply
implicitly assuming that groupby
is stable - that is, if a and b are instances of the same group, and pre-grouping, a appeared before b, then a will appear pre b following the grouping as well.
I think there are several answers clearly implicitly using this, but, to be concrete, here is one using groupby
+cumsum
.
Is there anything actually promising this behavior? The documentation only states:
Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns.
Also, pandas having indices, the functionality could be theoretically be achieved also without this guarantee (albeit in a more cumbersome way).