CLN: _wrap_applied_output
The majority of this change is the result of three operations:

1. Move code that returns up top, to avoid the heavily nested structure.
2. Move variable creation as close as possible to where they are used. Previously, in certain cases computations were being done and then going unused.
3. Combine duplicated code.

There are two sections that I was able to entirely remove:

````
ping = self.grouper.groupings[0]
if len(keys) == ping.ngroups:
    key_index = ping.group_index
    key_index.name = key_names[0]

    key_lookup = Index(keys)
    indexer = key_lookup.get_indexer(key_index)

    # reorder the values
    values = [values[i] for i in indexer]

    # update due to the potential reorder
    first_not_none = next(com.not_none(*values), None)
````

and

````
# GH5788 instead of stacking; concat gets the
# dtypes correct
from pandas.core.reshape.concat import concat

result = concat(
    values,
    keys=key_index,
    names=key_index.names,
    axis=self.axis,
).unstack()
result.columns = index
````

For the single test that I touched, it was checking that the categorical dtype of an index was being dropped after a groupby. I don't believe that is the correct behavior - that the categorical dtype should remain. I checked groupby/categorical bugs and didn't find any issues this closes.
Codecov is finding a lot in this method that isn't hit. I'm going to see what tests can be added or branches that can be removed. > The majority of this change is the result of three operations:

To the extent that you can split this into independent pieces, it will be easier to review.