REF: de-duplicate groupby_helper code
There's one other piece of de-duplication I think should be feasible but cython is still raising compilation errors for, so will do separately.

Orthogonal to #28931, but I expect it will cause merge conflicts.  28931 should be a higher priority.