REF: Make function for dtype casting in pre-processing of groupby
#### Is your feature request related to a problem?

[this should provide a description of what the problem is, e.g. "I wish I could use pandas to do [...]"]

#### Describe the solution you'd like

As mentioned in #37145 the pre processing part in https://github.com/pandas-dev/pandas/blob/6abe5106a63431239081af7c15a1569628cf7628/pandas/core/groupby/groupby.py#L2174 is very generic and could be moved to a function

