CLN GH22873 Replace base excepts in pandas/core
State: closed

This cleaning fix adds specific exception classes to bare except statements to correct PEP8 linting errors. The classes were chosen by a mixture of educated guessing (i.e. if a statement gets the `length` property, it might raise an `AttributeError`), trying out functions on known edge cases (i.e. giving a datetime input that is too large gives `tslibs.OutOfBoundsDatetime`), and inspecting functions in the `try` block to see what kind of errors they raise.

- [x] xref #22873
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas
name: CI
description: Continuous Integration
name: Code Style
description: Code style, linting, code_checks

Comments:
Hello @pambot! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`pandas/core/computation/pytables.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/computation/pytables.py) !

 - There are no PEP8 issues in the file [`pandas/core/dtypes/common.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/dtypes/common.py) !

 - There are no PEP8 issues in the file [`pandas/core/dtypes/dtypes.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/dtypes/dtypes.py) !

 - There are no PEP8 issues in the file [`pandas/core/frame.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/frame.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/frozen.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/indexes/frozen.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/multi.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/indexes/multi.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexing.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/indexing.py) !

 - There are no PEP8 issues in the file [`pandas/core/internals/blocks.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/internals/blocks.py) !

 - There are no PEP8 issues in the file [`pandas/core/nanops.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/nanops.py) !

 - There are no PEP8 issues in the file [`pandas/core/ops.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/ops.py) !

 - There are no PEP8 issues in the file [`pandas/core/sparse/array.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/sparse/array.py) !

 - There are no PEP8 issues in the file [`pandas/core/tools/datetimes.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/tools/datetimes.py) !

 - There are no PEP8 issues in the file [`pandas/core/window.py`](https://github.com/pambot/pandas/blob/ae3d430e521dd81c3e66fee18c21a05a7bf2310c/pandas/core/window.py) !



##### Comment last updated on October 02, 2018 at 00:57 Hours UTC
@datapythonista My build is failing on `origin/master`, and I tried pulling from `upstream` just now, but it's still an issue. This could be something that is just happening to me, but I'm not sure what to do about it?

Specifically the tests are in `pandas/tests/plotting/test_datetimelike.py` and `pandas/tests/io/test_parquet.py`
If I'm not wrong, those were problems with the build unrelated to your PR. I think the datetime one is now fix in master, and should be ok if you merge the last master. The parquet one I think it was because of an amazon url, not sure if that's fixed, or it happens just some times. We need to take a look at it, don't worry much about it, if your changes are all right, we'll fix it eventually and merge your PR.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22901?src=pr&el=h1) Report
> Merging [#22901](https://codecov.io/gh/pandas-dev/pandas/pull/22901?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5ce06b5bdb8c44043c6463bf8ce3da758800a189?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `59.37%`.

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

```diff
@@            Coverage Diff             @@
##           master   #22901      +/-   ##
==========================================
- Coverage   92.19%   92.18%   -0.01%     
==========================================
  Files         169      169              
  Lines       50833    50834       +1     
==========================================
- Hits        46863    46861       -2     
- Misses       3970     3973       +3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.6% <59.37%> (-0.01%)` | :arrow_down: |
| #single | `42.37% <15.62%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22901?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/sparse/array.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc3BhcnNlL2FycmF5LnB5) | `91.38% <0%> (ø)` | :arrow_up: |
| [pandas/core/window.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvd2luZG93LnB5) | `96.4% <0%> (ø)` | :arrow_up: |
| [pandas/core/indexes/frozen.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9mcm96ZW4ucHk=) | `91.42% <0%> (ø)` | :arrow_up: |
| [pandas/core/computation/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tcHV0YXRpb24vcHl0YWJsZXMucHk=) | `92.37% <0%> (ø)` | :arrow_up: |
| [pandas/core/dtypes/dtypes.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2R0eXBlcy5weQ==) | `95.85% <100%> (-0.26%)` | :arrow_down: |
| [pandas/core/dtypes/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2NvbW1vbi5weQ==) | `95.02% <100%> (ø)` | :arrow_up: |
| [pandas/core/ops.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvb3BzLnB5) | `97.37% <100%> (ø)` | :arrow_up: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.2% <50%> (ø)` | :arrow_up: |
| [pandas/core/nanops.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvbmFub3BzLnB5) | `95.14% <50%> (ø)` | :arrow_up: |
| [pandas/core/internals/blocks.py](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW50ZXJuYWxzL2Jsb2Nrcy5weQ==) | `93.85% <60%> (ø)` | :arrow_up: |
| ... and [4 more](https://codecov.io/gh/pandas-dev/pandas/pull/22901/diff?src=pr&el=tree-more) | |

------

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

@jreback I addressed your comments - could you please take another look?
thanks @pambot very nice!
This seems to be the first commit with the failure on numpydev. https://travis-ci.org/pandas-dev/pandas/jobs/435916255#L2767

<details>

```
____________ TestSelection.test_groupby_duplicated_column_errormsg _____________
[gw0] linux -- Python 3.7.0 /home/travis/miniconda3/envs/pandas/bin/python
self = <pandas.tests.groupby.test_grouping.TestSelection object at 0x7f87673fb668>
    def test_groupby_duplicated_column_errormsg(self):
        # GH7511
        df = DataFrame(columns=['A', 'B', 'A', 'C'],
                       data=[range(4), range(2, 6), range(0, 8, 2)])
    
>       pytest.raises(ValueError, df.groupby, 'A')
pandas/tests/groupby/test_grouping.py:42: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
pandas/core/generic.py:7136: in groupby
    observed=observed, **kwargs)
pandas/core/groupby/groupby.py:1966: in groupby
    return klass(obj, by, **kwds)
pandas/core/groupby/groupby.py:365: in __init__
    mutated=self.mutated)
pandas/core/groupby/grouper.py:588: in _get_grouper
    if is_categorical_dtype(gpr) and len(gpr) != obj.shape[axis]:
pandas/core/dtypes/common.py:574: in is_categorical_dtype
    return CategoricalDtype.is_dtype(arr_or_dtype)
pandas/core/dtypes/base.py:93: in is_dtype
    return cls.construct_from_string(dtype) is not None
pandas/core/dtypes/dtypes.py:359: in construct_from_string
    if string == 'category':
pandas/core/ops.py:1885: in f
    try_cast=False)
pandas/core/frame.py:4927: in _combine_const
    return ops.dispatch_to_series(self, other, func)
pandas/core/ops.py:1678: in dispatch_to_series
    new_data = expressions.evaluate(column_op, str_rep, left, right)
pandas/core/computation/expressions.py:205: in evaluate
    return _evaluate(op, op_str, a, b, **eval_kwargs)
pandas/core/computation/expressions.py:65: in _evaluate_standard
    return op(a, b)
pandas/core/ops.py:1658: in column_op
    for i in range(len(a.columns))}
pandas/core/ops.py:1658: in <dictcomp>
    for i in range(len(a.columns))}
pandas/core/ops.py:1510: in wrapper
    res = na_op(values, other)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
x = array([0, 2, 0]), y = 'category'
    def na_op(x, y):
        # TODO:
        # should have guarantess on what x, y can be type-wise
        # Extension Dtypes are not called here
    
        # Checking that cases that were once handled here are no longer
        # reachable.
        assert not (is_categorical_dtype(y) and not is_scalar(y))
    
        if is_object_dtype(x.dtype):
            result = _comp_method_OBJECT_ARRAY(op, x, y)
    
        elif is_datetimelike_v_numeric(x, y):
            return invalid_comparison(x, y, op)
    
        else:
    
            # we want to compare like types
            # we only want to convert to integer like if
            # we are not NotImplemented, otherwise
            # we would allow datetime64 (but viewed as i8) against
            # integer comparisons
    
            # we have a datetime/timedelta and may need to convert
            assert not needs_i8_conversion(x)
            mask = None
            if not is_scalar(y) and needs_i8_conversion(y):
                mask = isna(x) | isna(y)
                y = y.view('i8')
                x = x.view('i8')
    
            method = getattr(x, op_name, None)
            if method is not None:
                with np.errstate(all='ignore'):
>                   result = method(y)
E                   FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
```

</details>

Moving further discussion to https://github.com/pandas-dev/pandas/issues/22960