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..837b01974be93 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1,22 +1,20 @@ import importlib -from typing import List, Type # noqa import warnings +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 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 -except ImportError: - pass +import_optional_dependency("pandas.plotting._matplotlib", raise_on_missing=False) def hist_series( @@ -732,7 +730,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 +1601,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..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 +from typing import Optional 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