BUG: Fix groupby with MultiIndex Series corner case (#25704)
State: closed

- [x] closes #25704
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

There was an issue with `groupby` when you used `groupby` on a `Series` with a list of labels that was equal to the length of the entire `Series`.

`core/groupby/grouper.py:_get_grouper` was not properly handling the case where the list of labels provided were all in the `Series` index names.  The `DataFrame` case was being handled, but `Series` was previously missed in this case.

Labels:
name: Bug
name: Groupby
name: MultiIndex

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=h1) Report
> Merging [#25743](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/65c0441a41b2dcaeebb648274d30978419a8661a?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #25743      +/-   ##
==========================================
+ Coverage   91.25%   91.25%   +<.01%     
==========================================
  Files         172      172              
  Lines       52977    52979       +2     
==========================================
+ Hits        48343    48348       +5     
+ Misses       4634     4631       -3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.83% <100%> (ø)` | :arrow_up: |
| #single | `41.74% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/grouper.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9ncm91cGVyLnB5) | `98.18% <100%> (-0.36%)` | :arrow_down: |
| [pandas/core/indexes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9iYXNlLnB5) | `96.78% <0%> (+0.21%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25743?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/25743?src=pr&el=footer). Last update [65c0441...6d0dfc1](https://codecov.io/gh/pandas-dev/pandas/pull/25743?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/25743?src=pr&el=h1) Report
> Merging [#25743](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/f0ba498f71a9aed5b9cfda6ad6099fe9e7292cda?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #25743      +/-   ##
==========================================
+ Coverage   91.81%   91.81%   +<.01%     
==========================================
  Files         175      175              
  Lines       52578    52580       +2     
==========================================
+ Hits        48273    48275       +2     
  Misses       4305     4305
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.36% <100%> (ø)` | :arrow_up: |
| #single | `41.89% <0%> (-0.08%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/grouper.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9ncm91cGVyLnB5) | `98.16% <100%> (-0.36%)` | :arrow_down: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `75% <0%> (-12.5%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.79% <0%> (-0.12%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `90.73% <0%> (+0.1%)` | :arrow_up: |
| [pandas/core/indexes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/25743/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9iYXNlLnB5) | `96.79% <0%> (+0.21%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25743?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/25743?src=pr&el=footer). Last update [f0ba498...322b2ce](https://codecov.io/gh/pandas-dev/pandas/pull/25743?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

lgtm. over to you @WillAyd 
can update to @WillAyd comments
@ArtificialQualia can you merge master and update to comments
@WillAyd sorry, I had commented on your issue a while ago but I guess it wasn't showing up correctly since it was under my 'review'.  I deleted the review and re-added my comment to you.
Nice work @ArtificialQualia !
This may have caused a perf regression: http://pandas.pydata.org/speed/pandas/html/index.html#stat_ops.SeriesMultiIndexOps.time_op?p-op=%27sem%27&p-level=1&commits=819fc1525488b4da7f3d76051a85971a2e004fed

I haven’t looked closely. 
Thanks for the callout - @ArtificialQualia could you take a look?
@WillAyd it does appear this caused the regression due to those functions calling `_get_grouper` and then hitting `all(g in obj.index.names for g in keys)` when they didn't have to do that before.

However, with some refactoring of the `if` statements I can get this back down to the previous performance numbers (even slightly better in some cases).  Do you want a separate issue/PR for that fix?
Yea sure separate PR makes sense