BUG: Groupby selection context not being properly reset
- [x] closes #28523 
- [ ] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Will this fix #26864? So there are two tests that will need to be changed regarding this fix:

[`test_grouper_creation_bug`](https://github.com/pandas-dev/pandas/blob/d4e2734ffb7a13fdf10f0ed8ad59521841e8ed07/pandas/tests/groupby/test_grouping.py#L164) and [`test_agg_timezone_round_trip`](https://github.com/pandas-dev/pandas/blob/master/pandas/tests/groupby/aggregate/test_other.py#L457)

The second change is straightforward, the first might have identified another bug.

Is this behavior expected?

```
>>> df = DataFrame({"A": [0, 0, 1, 1, 2, 2], "B": [1, 2, 3, 4, 5, 6]})
>>> df.groupby('A').sum()
    B
A
0   3
1   7
2  11
>>> df.groupby('A').apply(lambda x: x.sum())
   A   B
A
0  0   3
1  2   7
2  4  11
```

The mutating state caused those two to be equal in the test, but now it fails, and I'm not sure if the second is intended behavior. I think the second is a bug might have open issue already

Sent from my iPhone

> On Sep 19, 2019, at 6:45 PM, Chris Zimmerman <notifications@github.com> wrote:
> 
> ﻿
> So there are two tests that will need to be changed regarding this fix:
> 
> test_grouper_creation_bug and test_agg_timezone_round_trip
> 
> The second change is straightforward, the first might have identified another bug.
> 
> Is this behavior expected?
> 
> >>> df = DataFrame({"A": [0, 0, 1, 1, 2, 2], "B": [1, 2, 3, 4, 5, 6]})
> >>> df.groupby('A').sum()
>     B
> A
> 0   3
> 1   7
> 2  11
> >>> df.groupby('A').apply(lambda x: x.sum())
>    A   B
> A
> 0  0   3
> 1  2   7
> 2  4  11
> The mutating state caused those two to be equal in the test, but now it fails, and I'm not sure if the second is intended behavior.
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.
 #22546 #22542 #20420 might all be solved by this @WillAyd OK, I'll track down that bug and take care of that before I do anything else with this PR.  Also per your previous comment, making the test work with `reduction_func` removed the last instance of the improper set selection, so I think the context manager is now used everywhere. Sounds good - nice work so far! #17091 *might* be closed by this as well @christopherzimmerman can you merge master, been some changed in groupby recently. @christopherzimmerman can you rebase, this would be really nice to see fixed Hello @christopherzimmerman! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:









There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2019-10-21 13:45:44 UTC @jbrockmendel rebased and cleaned up what I was working on.  https://github.com/pandas-dev/pandas/issues/28549 still needs to be addressed before this is green.  I had a PR going for that issue and I'll revisit it. a lot of work has been going on in these files; can you rebase again?  ill try to review in a timely manner so we dont have to go through another round @christopherzimmerman would be a nice change, can you merge master I will merge, but #29131 will need to be merged first or else the CI will fail, since this requires some test updates from the other PR. Let's come back to this after the other PR is merged then; (looks like more changes are needed there).

Closing to reduce PR queue for the time being - thanks as always!