STYLE: Specify bare exceptions in pandas/tests
State: closed

- [x] closes #22872
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

The empty `except` statements now capture specific exceptions.

Following warnings are fixed:
```
pandas/tests/test_panel.py:338:13: E722 do not use bare 'except'
pandas/tests/test_panel.py:344:13: E722 do not use bare 'except'
pandas/tests/test_nanops.py:144:9: E722 do not use bare 'except'
pandas/tests/test_nanops.py:149:9: E722 do not use bare 'except'
pandas/tests/test_nanops.py:170:21: E722 do not use bare 'except'
pandas/tests/test_nanops.py:174:21: E722 do not use bare 'except'
pandas/tests/test_multilevel.py:1381:9: E722 do not use bare 'except'
pandas/tests/test_strings.py:2632:13: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:54:9: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:62:5: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:120:5: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:4624:21: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:1793:9: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:2378:9: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:2405:9: E722 do not use bare 'except'
pandas/tests/io/formats/test_format.py:73:5: E722 do not use bare 'except'
pandas/tests/io/formats/test_format.py:455:13: E722 do not use bare 'except'
pandas/tests/indexing/common.py:154:13: E722 do not use bare 'except'
pandas/tests/indexing/common.py:217:17: E722 do not use bare 'except'
```

Labels:
name: Code Style
description: Code style, linting, code_checks
name: Clean

Comments:
Hello @alexander-ponomaroff! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`doc/source/conf.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/doc/source/conf.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexing/common.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/indexing/common.py) !

 - There are no PEP8 issues in the file [`pandas/tests/io/formats/test_format.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/io/formats/test_format.py) !

 - There are no PEP8 issues in the file [`pandas/tests/io/test_pytables.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/io/test_pytables.py) !

 - There are no PEP8 issues in the file [`pandas/tests/io/test_sql.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/io/test_sql.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_multilevel.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/test_multilevel.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_nanops.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/test_nanops.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_panel.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/test_panel.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_strings.py`](https://github.com/alexander-ponomaroff/pandas/blob/ff36b216284ea77acd6927c96468afcada78b37b/pandas/tests/test_strings.py) !



##### Comment last updated on November 13, 2018 at 04:43 Hours UTC
I think this is the last chunk of bare exceptions. Can you run `flake8 --select=E722 --config=none .` in your branch, and if nothing is being reported, edit `setup.cfg` and `pep8speaks.yml` to stop ignoring `E722` please?
@datapythonista 

When running: flake8 --select=E722 --config=none pandas/tests/
Nothing is reported

When running: flake8 --select=E722 --config=none 
./doc/source/conf.py:568:9: E722 do not use bare 'except'
./doc/source/conf.py:573:5: E722 do not use bare 'except'
./doc/source/conf.py:580:5: E722 do not use bare 'except'

Should I fix those also?
Yes, that would be great. This will finish all them and we can avoid new bare excepts using the CI from now on with the changes I mentioned.
@datapythonista I had some trouble with pandas/tests/indexing/common.py - line 217. Could not figure out which exceptions needed to go there. My previous commit looks like passed the first CI, so it's looking promising, although I have 4 different exceptions on line 217.

I put the final exceptions in doc/source and removed E722 from the ignore lists. Hopefully it passes all the CI tests.

If you have some advice about line 217 of pandas/tests/indexing/common.py, it would be much appreciated. That's the only one that gave me some trouble and I don't think all 4 of those exceptions are correct. I couldn't get pytest to run, so had to wait for CI to perform testing.
Can you remove `E722` from the ignores in `pep8speaks.yml` too please?

I don't have access to a computer at the moment, @jreback, can you check if the exceptions in `common.py L217` make sense to you?
@datapythonista Weird, I removed it from `pep8speaks.yml` but it didn't get added to the commit.

https://travis-ci.org/pandas-dev/pandas/jobs/447018587

I am unsure why I fail this check, it doesn't show any errors except this:
The command "ci/code_checks.sh" exited with 1.

Also, it looks like that line 217 of common.py is still not working properly as it fails one of the tests of Travis CI, so I will put `except: Exception` on that line for now.
https://travis-ci.org/pandas-dev/pandas/jobs/447018588

Other than these two tests, everything else passes.
can you rebase
@alexander-ponomaroff the error in travis looks like a genuine error introduced in your changes, I think pymysql needs to be imported. Can you take a look and leave the CI green so we can merge. Thanks!
@alexander-ponomaroff do you have time to fix the CI problem?

Yes, I will work on this tonight. I was away from a computer on Friday and Saturday.


> On Nov 4, 2018, at 12:21 PM, Marc Garcia <notifications@github.com> wrote:
> 
> @alexander-ponomaroff do you have time to fix the CI problem?
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.

@datapythonista My checks passed after updated pymysql. I had to resolve a merge conflict after, so hopefully nothing messes up and the checks pass again.
I think the linting `ci/code_checks.py lint` will fail with a pep8 error because of the import, as I mentioned. Let's see if we can leave it in green, and I'll try to get it merged asap, so you don't have to fix more conflicts.
@datapythonista Sorry about another delay. I was away for a conference. I will fix everything tomorrow and should be available from then on if any more problems arise. 

PS. I put the import statement there because that's how I saw it done throughout the file.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23370?src=pr&el=h1) Report
> Merging [#23370](https://codecov.io/gh/pandas-dev/pandas/pull/23370?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/adc54fe6a48483defab173d3af986fe47bb5e3eb?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

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

```diff
@@           Coverage Diff           @@
##           master   #23370   +/-   ##
=======================================
  Coverage   92.25%   92.25%           
=======================================
  Files         161      161           
  Lines       51237    51237           
=======================================
  Hits        47269    47269           
  Misses       3968     3968
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.63% <ø> (ø)` | :arrow_up: |
| #single | `42.29% <ø> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/23370?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/23370/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9iYXNlLnB5) | `96.45% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23370?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/23370?src=pr&el=footer). Last update [adc54fe...c55692f](https://codecov.io/gh/pandas-dev/pandas/pull/23370?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/23370?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@960a73f`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff            @@
##             master   #23370   +/-   ##
=========================================
  Coverage          ?   92.24%           
=========================================
  Files             ?      161           
  Lines             ?    51433           
  Branches          ?        0           
=========================================
  Hits              ?    47446           
  Misses            ?     3987           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.64% <ø> (?)` | |
| #single | `42.28% <ø> (?)` | |


------

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

lgtm, let's see if it finishes on green
looks like still a linting error: 
```
Linting .py code
./pandas/tests/io/test_sql.py:1819:1: W293 blank line contains whitespace
```
@datapythonista does this fully close #22872 . ?
> @datapythonista does this fully close #22872 . ?

Yes, this is actually the last chunk of bare except fixes. We remove the ignore of the flake8 error here, so we start validating that no more are added in this PR.
@datapythonista All checks pass now.
Looks great. Thanks for the fixes @alexander-ponomaroff 

@jreback, can you take a look and merge if you're happy?
lgtm. just merged master to be sure. let's merge on green.
rebased. can merge on green.
thanks @alexander-ponomaroff 
@jreback @datapythonista No problem guys :)