Raise error in usecols when column doesn't exist but length matches
State: closed

 - [ ] closes #14671
 - [ ] tests added / passed
 - [ ] passes ``git diff upstream/master --name-only -- '*.py' | flake8 --diff``
 - [ ] Bug fix: In parser.py added check that usecols option in read_csv raises an error if it contains strings not in the names option. 'test_raise_on_usecols_names_mismatch' added to tests/io/parser/usecols.py


Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas
name: IO CSV
description: read_csv, to_csv

Comments:
There will be some warnings from our style checker:

```
pandas/tests/io/parser/usecols.py:481:9: E266 too many leading '#' for block comment
pandas/tests/io/parser/usecols.py:483:45: E262 inline comment should start with '# '
pandas/tests/io/parser/usecols.py:484:32: E261 at least two spaces before inline comment
pandas/tests/io/parser/usecols.py:484:33: E262 inline comment should start with '# '
pandas/tests/io/parser/usecols.py:486:23: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:486:27: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:486:31: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:488:38: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:488:50: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:488:62: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:488:74: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:491:23: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:491:27: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:491:31: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:495:23: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:495:27: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:496:80: E501 line too long (91 > 79 characters)
pandas/tests/io/parser/usecols.py:502:38: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:502:50: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:502:62: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:502:74: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:506:80: E501 line too long (84 > 79 characters)
pandas/tests/io/parser/usecols.py:511:80: E501 line too long (84 > 79 characters)
pandas/tests/io/parser/usecols.py:516:9: E303 too many blank lines (2)
pandas/tests/io/parser/usecols.py:516:23: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:516:27: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:516:31: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:517:80: E501 line too long (91 > 79 characters)
pandas/tests/io/parser/usecols.py:518:80: E501 line too long (81 > 79 characters)
pandas/tests/io/parser/usecols.py:519:23: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:519:27: E231 missing whitespace after ','
pandas/tests/io/parser/usecols.py:520:80: E501 line too long (91 > 79 characters)
```

you can get the warnings with `flake8 pandas/tests/io/parser/usecols.py` (may have to `pip install flake8`)

Could you also add a release note to `doc/source/whatsnew/v0.20.2.txt` under the bug fixes seciton?
can you add a whatsnew entry for 0.20.2 (IO section)
@bpraggastis do you have time to update this today or tomorrow? We're releasing 0.20.2 by the end of the week.
@TomAugspurger @jreback : If @bpraggastis does not respond by tomorrow, just ping me.  I can pull this branch down and finish it off.
@gfyoung if you are interested
@TomAugspurger @jreback Just now had a chance to check back in. Have you fixed all of the things you wanted changed or should I review your comments and respond?
@bpraggastis couple of comments, pls do those
add a whatsnew entry for 0.20.2
needs to pass CI
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=h1) Report
> Merging [#16460](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/92372c7632a92d7bd980534b5a78590b2d9453ad?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/16460/graphs/tree.svg?token=eZ4WkYLtcO&src=pr&width=650&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16460      +/-   ##
==========================================
+ Coverage   90.42%   90.42%   +<.01%     
==========================================
  Files         161      161              
  Lines       51024    51026       +2     
==========================================
+ Hits        46139    46141       +2     
  Misses       4885     4885
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/parsers.py](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhcnNlcnMucHk=) | `95.32% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16460?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/16460?src=pr&el=footer). Last update [92372c7...972d72b](https://codecov.io/gh/pandas-dev/pandas/pull/16460?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/16460?src=pr&el=h1) Report
> Merging [#16460](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/9e620bc2898d4eb1d8aa7a3a6c4f2a90d272e78e?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/16460/graphs/tree.svg?width=650&height=150&src=pr&token=eZ4WkYLtcO)](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16460      +/-   ##
==========================================
+ Coverage   90.92%   90.92%   +<.01%     
==========================================
  Files         161      161              
  Lines       49240    49242       +2     
==========================================
+ Hits        44772    44775       +3     
+ Misses       4468     4467       -1
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/parsers.py](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhcnNlcnMucHk=) | `95.43% <100%> (ø)` | :arrow_up: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/16460?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `95.33% <0%> (+0.09%)` | :arrow_up: |

------

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

I pushed an update addressing the comments.
@TomAugspurger Thank you for closing this. 
Thank you for the contribution @bpraggastis!
@TomAugspurger Thank You for the assistance! I sent you an email with a couple of questions. Looking forward to hearing from you.