Research

  • [X] I have searched the [pandas] tag on StackOverflow for similar questions.

  • [X] I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/70842600/

Question about pandas

Sample code is provided in the SO post.

I can reproduce this problem with pandas 1.2.4 and 1.4.0.

In my opinion, this is either a design flaw or should be documented with more clarity. Other opinions?

Comment From: rhshadrach

Thanks for the report! I believe what's happening is that when groupby(...).apply is inferring the operation is a transform, the sort is ignored (and the group keys aren't included). This appears to be the correct behavior to me, the result of a transform should have an identical index to the input. That said, a line to the effect "This argument is ignored when the operation is inferred to be a transform" should be added.

In #34998, supplying group_keys=True makes pandas include the group_keys and respects the sort argument.

Comment From: rhshadrach

That said, a line to the effect "This argument is ignored when the operation is inferred to be a transform" should be added.

This now appears in the docs. Closing.