Fix GroupBy nth Handling with Observed=False
State: closed

- [X] closes #26385
- [X] tests added / passed
- [] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [X] whatsnew entry


Labels:
name: Groupby
name: Missing-data
description: np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
name: Categorical
description: Categorical Data Type

Comments:
Hello @WillAyd! 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-08-19 17:22:58 UTC
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=h1) Report
> Merging [#26419](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/80e261555adc2f58930f2dad6c765f25e7dc8e08?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26419/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26419      +/-   ##
==========================================
- Coverage   91.69%   91.68%   -0.01%     
==========================================
  Files         174      174              
  Lines       50739    50742       +3     
==========================================
- Hits        46523    46522       -1     
- Misses       4216     4220       +4
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.19% <100%> (ø)` | :arrow_up: |
| #single | `41.16% <20%> (-0.16%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9ncm91cGJ5LnB5) | `97.25% <100%> (+0.01%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.02% <0%> (-0.12%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=footer). Last update [80e2615...f671204](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=h1) Report
> Merging [#26419](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/e9555159afd8caabc88b45ca6c06a16efe2f6afc?src=pr&el=desc) will **decrease** coverage by `0.31%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26419/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26419      +/-   ##
==========================================
- Coverage   92.04%   91.72%   -0.32%     
==========================================
  Files         180      174       -6     
  Lines       50714    50757      +43     
==========================================
- Hits        46679    46559     -120     
- Misses       4035     4198     +163
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.23% <100%> (-0.45%)` | :arrow_down: |
| #single | `41.69% <20%> (-0.27%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9ncm91cGJ5LnB5) | `97.25% <100%> (+0.07%)` | :arrow_up: |
| [pandas/plotting/\_misc.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19taXNjLnB5) | `38.23% <0%> (-26.63%)` | :arrow_down: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-21.06%)` | :arrow_down: |
| [pandas/io/gcs.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2djcy5weQ==) | `80% <0%> (-20%)` | :arrow_down: |
| [pandas/io/s3.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3MzLnB5) | `89.47% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/groupby/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9iYXNlLnB5) | `91.83% <0%> (-8.17%)` | :arrow_down: |
| [pandas/plotting/\_core.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb3JlLnB5) | `83.89% <0%> (-5.49%)` | :arrow_down: |
| [pandas/io/excel/\_xlrd.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2V4Y2VsL194bHJkLnB5) | `94.54% <0%> (-5.46%)` | :arrow_down: |
| [pandas/io/clipboard/clipboards.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2NsaXBib2FyZC9jbGlwYm9hcmRzLnB5) | `31.64% <0%> (-3.14%)` | :arrow_down: |
| [pandas/core/internals/managers.py](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW50ZXJuYWxzL21hbmFnZXJzLnB5) | `93.93% <0%> (-2.93%)` | :arrow_down: |
| ... and [102 more](https://codecov.io/gh/pandas-dev/pandas/pull/26419/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=footer). Last update [e955515...ad729c5](https://codecov.io/gh/pandas-dev/pandas/pull/26419?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

I think this makes sense to wait until #26463 gets merged and use `_reindex_like` from that once it gets moved into the base GroupBy class
So doesn't look like merging in those changes helped; I think we have a few random reindexing ops scattered throughout the module still investigating better ways to share that functionality
what's the status here?
Haven't been able to address this comment yet https://github.com/pandas-dev/pandas/pull/26419/files#r285344403 so depending on how much of a blocker you think that is may or may not be ready for merge.

I think as is gets us a lot closer to unifying nth / first / last so ultimately net benefit but open to debate
@WillAyd linting issue: https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=14466&view=logs&jobId=435ca956-9126-505f-566f-fff31072e2ba&taskId=ffc59b44-7438-5626-aa7a-09fa4141874b&lineStart=43&lineEnd=44&colStart=1&colEnd=1
@WillAyd what was that comment above? this PR looks good.
https://github.com/pandas-dev/pandas/pull/26419/commits/ee549ed668d485d342c5ed9589eaea8327bb7401#r285344403
Hmm link to comments in old commits might not work but if you look at the entire commit you'll see it as the first one [there](https://github.com/pandas-dev/pandas/pull/26419/commits/ee549ed668d485d342c5ed9589eaea8327bb7401#r285344403)


Fixed the merge conflict.
Thanks @WillAyd. I think this is an improvement on master.

I believe there's an outstanding request for additional test coverage with `observed=True`? Can you double check that when you have time, and perhaps open an issue for it?
Thanks @TomAugspurger for pushing this over the finish line