From f323b2cb3e53fa8079aa1151363d80c82d23c075 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 5 Sep 2019 18:33:47 -0700 Subject: [PATCH 1/4] CLN: catch Exception less --- pandas/core/apply.py | 14 ++++++++------ pandas/core/arrays/datetimes.py | 3 ++- pandas/core/dtypes/concat.py | 5 ++--- pandas/core/indexes/accessors.py | 2 +- pandas/core/series.py | 3 --- pandas/plotting/_core.py | 10 +++++----- pandas/plotting/_matplotlib/converter.py | 4 +++- pandas/plotting/_matplotlib/core.py | 2 +- setup.py | 4 ++-- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index b96b3c7572031..e6766a33a613b 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -199,20 +199,21 @@ def apply_empty_result(self): return self.obj.copy() # we may need to infer - reduce = self.result_type == "reduce" + should_reduce = self.result_type == "reduce" from pandas import Series - if not reduce: + if not should_reduce: EMPTY_SERIES = Series([]) try: r = self.f(EMPTY_SERIES, *self.args, **self.kwds) - reduce = not isinstance(r, Series) except Exception: pass + else: + should_reduce = not isinstance(r, Series) - if reduce: + if should_reduce: return self.obj._constructor_sliced(np.nan, index=self.agg_axis) else: return self.obj.copy() @@ -306,10 +307,11 @@ def apply_series_generator(self): for i, v in enumerate(series_gen): try: results[i] = self.f(v) - keys.append(v.name) - successes.append(i) except Exception: pass + else: + keys.append(v.name) + successes.append(i) # so will work with MultiIndex if len(successes) < len(res_index): diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 732f819e743a4..5dff1f93264c3 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2284,7 +2284,8 @@ def _infer_tz_from_endpoints(start, end, tz): """ try: inferred_tz = timezones.infer_tzinfo(start, end) - except Exception: + except AssertionError: + # infer_tzinfo raises AssertionError if passed mismatched timezones raise TypeError( "Start and end cannot both be tz-aware with different timezones" ) diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index 12f3fd2c75dc8..1094ab22238e9 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -89,10 +89,9 @@ def concat_compat(to_concat, axis=0): # filter empty arrays # 1-d dtypes always are included here def is_nonempty(x): - try: - return x.shape[axis] > 0 - except Exception: + if x.ndim <= axis: return True + return x.shape[axis] > 0 # If all arrays are empty, there's nothing to convert, just short-cut to # the concatenation, #3121. diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 2036728e702f3..11b6cb2ca3ed4 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -316,7 +316,7 @@ def __new__(cls, data): # do all the validation here. from pandas import Series - if not isinstance(data, Series): + if not isinstance(data, ABCSeries): raise TypeError( "cannot convert an object of type {0} to a " "datetimelike index".format(type(data)) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6fb39c422de93..10d50e89ca92e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1114,9 +1114,6 @@ def __getitem__(self, key): return self.__getitem__(new_key) raise - except Exception: - raise - if is_iterator(key): key = list(key) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index d3c9e8ccfa51c..ec237c4514595 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1,20 +1,20 @@ import importlib -from typing import List, Type # noqa import warnings +from pandas._config import get_option + from pandas.util._decorators import Appender from pandas.core.dtypes.common import is_integer, is_list_like from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries -import pandas from pandas.core.base import PandasObject # Trigger matplotlib import, which implicitly registers our # converts. Implicit registration is deprecated, and when enforced # we can lazily import matplotlib. try: - import pandas.plotting._matplotlib # noqa + import pandas.plotting._matplotlib # noqa:F401 except ImportError: pass @@ -732,7 +732,7 @@ def __call__(self, *args, **kwargs): # `x` parameter, and return a Series with the parameter `y` as values. data = self._parent.copy() - if isinstance(data, pandas.core.dtypes.generic.ABCSeries): + if isinstance(data, ABCSeries): kwargs["reuse_plot"] = True if kind in self._dataframe_kinds: @@ -1603,7 +1603,7 @@ def _get_plot_backend(backend=None): The backend is imported lazily, as matplotlib is a soft dependency, and pandas can be used without it being installed. """ - backend = backend or pandas.get_option("plotting.backend") + backend = backend or get_option("plotting.backend") if backend == "matplotlib": # Because matplotlib is an optional dependency and first-party backend, diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py index 893854ab26e37..446350cb5d915 100644 --- a/pandas/plotting/_matplotlib/converter.py +++ b/pandas/plotting/_matplotlib/converter.py @@ -329,7 +329,7 @@ def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"): class PandasAutoDateLocator(dates.AutoDateLocator): def get_locator(self, dmin, dmax): - "Pick the best locator based on a distance." + """Pick the best locator based on a distance.""" _check_implicitly_registered() delta = relativedelta(dmax, dmin) @@ -382,6 +382,7 @@ def __call__(self): dmax, dmin = dmin, dmax # We need to cap at the endpoints of valid datetime + # FIXME: dont leave commented-out # TODO(wesm) unused? # delta = relativedelta(dmax, dmin) # try: @@ -448,6 +449,7 @@ def autoscale(self): # We need to cap at the endpoints of valid datetime + # FIXME: dont leave commented-out # TODO(wesm): unused? # delta = relativedelta(dmax, dmin) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 6ff3f28440303..9d0e71f860d8e 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1,5 +1,5 @@ import re -from typing import Optional # noqa +from typing import Optional # noqa:F401 import warnings import numpy as np diff --git a/setup.py b/setup.py index a86527ace092b..76db96870c36a 100755 --- a/setup.py +++ b/setup.py @@ -300,12 +300,12 @@ def run(self): for clean_me in self._clean_me: try: os.unlink(clean_me) - except Exception: + except OSError: pass for clean_tree in self._clean_trees: try: shutil.rmtree(clean_tree) - except Exception: + except OSError: pass From b391e79eed2c5cffded515f318c0fc905f9dca6f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 6 Sep 2019 09:33:22 -0700 Subject: [PATCH 2/4] remove unnecessary noqa --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 9d0e71f860d8e..346949cb82c4d 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1,5 +1,5 @@ import re -from typing import Optional # noqa:F401 +from typing import Optional import warnings import numpy as np From 82ace67e4f15a6c3a24a379a222f8d0f2a9a2e90 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 6 Sep 2019 09:36:54 -0700 Subject: [PATCH 3/4] use import_optional_dependency --- pandas/plotting/_core.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index ec237c4514595..0fff8ecbf362b 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -3,6 +3,7 @@ from pandas._config import get_option +from pandas.compat._optional import import_optional_dependency from pandas.util._decorators import Appender from pandas.core.dtypes.common import is_integer, is_list_like @@ -13,10 +14,7 @@ # Trigger matplotlib import, which implicitly registers our # converts. Implicit registration is deprecated, and when enforced # we can lazily import matplotlib. -try: - import pandas.plotting._matplotlib # noqa:F401 -except ImportError: - pass +import_optional_dependency("pandas.plotting._matplotlib") def hist_series( From 40a7de4254e53474eb5b7f0ab5c5a819796aee69 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 6 Sep 2019 10:45:40 -0700 Subject: [PATCH 4/4] dont raise on misssing --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0fff8ecbf362b..837b01974be93 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -14,7 +14,7 @@ # Trigger matplotlib import, which implicitly registers our # converts. Implicit registration is deprecated, and when enforced # we can lazily import matplotlib. -import_optional_dependency("pandas.plotting._matplotlib") +import_optional_dependency("pandas.plotting._matplotlib", raise_on_missing=False) def hist_series(