REGR: fix case all-NaN/numeric object column in groupby 
State: closed

Fixes #39329

This basically reverts #35841, although the code itself has changed quite a bit since then, so it's simpler here as the original PR² (not dealing with blocks anymore)

The original code comment that was removed in #35841 *"We've split an object block!"* was correct, as it can actually happen in those example cases. 

Labels:
name: Groupby
name: Regression
description: Functionality that used to work in a prior pandas version

Comments:
This is a quite straightforward revert of https://github.com/pandas-dev/pandas/pull/35841, a clean-up PR in which was said "we need to identify a test case in which this doesnt work". And so I have added a few tests that actually do result in multiple bocks in that code path. 
Also, you can only end up in the new code for cases that would otherwise have led to an AssertionError, so I think this is safe to include for 1.2.2
we had this discussion before
about late PRs

appreciate them but it's too late
and 1.2.3 is appropriate 


@simonjayhawkins is not releasing today, so there is still a bit of time. Other PRs have been merged today as well. If it's not ready by tomorrow, then fine that it's for 1.2.3, but until then this can be assumed for 1.2.2 IMO (not as a blocker, but as "include if merged by releasing").
cc @jbrockmendel the tests added here are example cases of how you can end up with multiple blocks in this code path. Basically if you have multiple object dtype columns (a single block originally), but some column is entirely numeric, after the aggregation we infer the numeric dtype, and so you can end up with columns of different dtypes (and thus multiple blocks).
> but some column is entirely numeric

lets make sure to add this to the running list fo value-dependent behaviors
Since multiple people reviewed this, I think this can be merged?

(and to repeat: this has no impact whatsoever on code that already worked before)
thanks @jorisvandenbossche 
@meeseeksdev backport 1.2.x
Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

1. Checkout backport branch and update it.

```
$ git checkout 1.2.x
$ git pull
```

2. Cherry pick the first parent branch of the this PR on top of the older branch:
```
$ git cherry-pick -m1 e58a19340881d0122fdc9e3ec400adb891a1be69
```

3. You will likely have some merge/cherry-pick conflict here, fix them and commit:

```
$ git commit -am 'Backport PR #39655: REGR: fix case all-NaN/numeric object column in groupby '
```

4. Push to a named branch :

```
git push YOURFORK 1.2.x:auto-backport-of-pr-39655-on-1.2.x
```

5. Create a PR against branch 1.2.x, I would have named this PR:

> "Backport PR #39655 on branch 1.2.x"

And apply the correct labels and milestones.

Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon!

If these instruction are inaccurate, feel free to [suggest an improvement](https://github.com/MeeseeksBox/MeeseeksDev).
                
cc @simonjayhawkins @jorisvandenbossche for manual backport
> > but some column is entirely numeric
>
> lets make sure to add this to the running list fo value-dependent behaviors

@jbrockmendel I think this is more a case of "we liberally try to infer object dtype" (although of course those are strictly also a kind of value dependent behaviour, but for object dtype we have plenty of that)
@jorisvandenbossche the conflict is 
```
<<<<<<< HEAD
            assert len(result._mgr.blocks) == 1

            # unwrap DataFrame to get array
            result = result._mgr.blocks[0].values
            return result
=======
            mgr = result._mgr
            assert isinstance(mgr, BlockManager)

            # unwrap DataFrame to get array
            if len(mgr.blocks) != 1:
                # We've split an object block! Everything we've assumed
                # about a single block input returning a single block output
                # is a lie. See eg GH-39329
                return mgr.as_array()
            else:
                result = mgr.blocks[0].values
                return result
>>>>>>> e58a193408... REGR: fix case all-NaN/numeric object column in groupby  (#39655)
```
are you happy to accept the incoming changes here. will also include changes from #36010, specifically the `assert isinstance(mgr, BlockManager)`
@simonjayhawkins I don't directly see the difference with the diff of this PR, the above seems exactly what I changed, so accepting the incoming changes in that conflict should be fine
> I don't directly see the difference with the diff of this PR

should be more visible on #39677, on 1.2.x we don't have the `mgr = result._mgr`, but I think this is worth having to avoid the duplication, but also comes with an extra assert that was not on 1.2.x