BUG: .reset_index() should raise with an invalid level name (GH20925)
State: closed

#20925 Raises appropriate error for Series.reset_index(level_name, drop=True) when index is flat and an invalid level is supplied

- [x] closes #20925 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Labels:
name: Bug
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: MultiIndex

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21016?src=pr&el=h1) Report
> Merging [#21016](https://codecov.io/gh/pandas-dev/pandas/pull/21016?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/d623ffd90f57abda2beb34d807da58ca95b3743d?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/21016/graphs/tree.svg?src=pr&width=650&token=eZ4WkYLtcO&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/21016?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #21016      +/-   ##
==========================================
+ Coverage   91.83%   91.83%   +<.01%     
==========================================
  Files         153      153              
  Lines       49497    49498       +1     
==========================================
+ Hits        45456    45458       +2     
+ Misses       4041     4040       -1
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21016?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/series.py](https://codecov.io/gh/pandas-dev/pandas/pull/21016/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc2VyaWVzLnB5) | `94.12% <100%> (+0.1%)` | :arrow_up: |

------

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

Could you add a test that the correct error and error message is raised? Use `tm.assert_raises_regex`
Hello @KalyanGokhale! Thanks for updating the PR.





Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on May 18, 2018 at 01:27 Hours UTC
@TomAugspurger have added the test - please tell if any further edits are needed.
I mistakenly wrote in an obsolete discussion, copying here:

> len(level) < self.index.nlevels test is needed

Right, hadn't considered that zero-levels ``MultiIndex``es can't exist.

The ``isinstance(self.index, MultiIndex)`` doesn't make much sense but it's OK to leave it, as it guarantees that ``pd.Series(range(4)).reset_index([], drop=True)`` doesn't fail (``pd.Index.droplevel()`` doesn't exist). Could you add a test case for this?

After that and a couple of minor fixes to comments, I think we're ready.
Looks all good to me, ready to merge after the whatsnew note is added.
Updated v0.23.1.txt under Bug Fixes > Indexing as:
*Bug in :meth:`Series.reset_index` where appropriate error was not raised with a non-named level (:issue:`20925`)*
@KalyanGokhale merged, thanks!