Skip to content

Commit 379cbe9

Browse files
jbrockmendelproost
authored andcommitted
CLN: catch Exception less (pandas-dev#28309)
1 parent 9c975d0 commit 379cbe9

File tree

9 files changed

+25
-26
lines changed

9 files changed

+25
-26
lines changed

pandas/core/apply.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,21 @@ def apply_empty_result(self):
199199
return self.obj.copy()
200200

201201
# we may need to infer
202-
reduce = self.result_type == "reduce"
202+
should_reduce = self.result_type == "reduce"
203203

204204
from pandas import Series
205205

206-
if not reduce:
206+
if not should_reduce:
207207

208208
EMPTY_SERIES = Series([])
209209
try:
210210
r = self.f(EMPTY_SERIES, *self.args, **self.kwds)
211-
reduce = not isinstance(r, Series)
212211
except Exception:
213212
pass
213+
else:
214+
should_reduce = not isinstance(r, Series)
214215

215-
if reduce:
216+
if should_reduce:
216217
return self.obj._constructor_sliced(np.nan, index=self.agg_axis)
217218
else:
218219
return self.obj.copy()
@@ -306,10 +307,11 @@ def apply_series_generator(self):
306307
for i, v in enumerate(series_gen):
307308
try:
308309
results[i] = self.f(v)
309-
keys.append(v.name)
310-
successes.append(i)
311310
except Exception:
312311
pass
312+
else:
313+
keys.append(v.name)
314+
successes.append(i)
313315

314316
# so will work with MultiIndex
315317
if len(successes) < len(res_index):

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,8 @@ def _infer_tz_from_endpoints(start, end, tz):
22842284
"""
22852285
try:
22862286
inferred_tz = timezones.infer_tzinfo(start, end)
2287-
except Exception:
2287+
except AssertionError:
2288+
# infer_tzinfo raises AssertionError if passed mismatched timezones
22882289
raise TypeError(
22892290
"Start and end cannot both be tz-aware with different timezones"
22902291
)

pandas/core/dtypes/concat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ def concat_compat(to_concat, axis=0):
8989
# filter empty arrays
9090
# 1-d dtypes always are included here
9191
def is_nonempty(x):
92-
try:
93-
return x.shape[axis] > 0
94-
except Exception:
92+
if x.ndim <= axis:
9593
return True
94+
return x.shape[axis] > 0
9695

9796
# If all arrays are empty, there's nothing to convert, just short-cut to
9897
# the concatenation, #3121.

pandas/core/indexes/accessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def __new__(cls, data):
316316
# do all the validation here.
317317
from pandas import Series
318318

319-
if not isinstance(data, Series):
319+
if not isinstance(data, ABCSeries):
320320
raise TypeError(
321321
"cannot convert an object of type {0} to a "
322322
"datetimelike index".format(type(data))

pandas/core/series.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,6 @@ def __getitem__(self, key):
11141114
return self.__getitem__(new_key)
11151115
raise
11161116

1117-
except Exception:
1118-
raise
1119-
11201117
if is_iterator(key):
11211118
key = list(key)
11221119

pandas/plotting/_core.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import importlib
2-
from typing import List, Type # noqa
32
import warnings
43

4+
from pandas._config import get_option
5+
6+
from pandas.compat._optional import import_optional_dependency
57
from pandas.util._decorators import Appender
68

79
from pandas.core.dtypes.common import is_integer, is_list_like
810
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
911

10-
import pandas
1112
from pandas.core.base import PandasObject
1213

1314
# Trigger matplotlib import, which implicitly registers our
1415
# converts. Implicit registration is deprecated, and when enforced
1516
# we can lazily import matplotlib.
16-
try:
17-
import pandas.plotting._matplotlib # noqa
18-
except ImportError:
19-
pass
17+
import_optional_dependency("pandas.plotting._matplotlib", raise_on_missing=False)
2018

2119

2220
def hist_series(
@@ -732,7 +730,7 @@ def __call__(self, *args, **kwargs):
732730
# `x` parameter, and return a Series with the parameter `y` as values.
733731
data = self._parent.copy()
734732

735-
if isinstance(data, pandas.core.dtypes.generic.ABCSeries):
733+
if isinstance(data, ABCSeries):
736734
kwargs["reuse_plot"] = True
737735

738736
if kind in self._dataframe_kinds:
@@ -1603,7 +1601,7 @@ def _get_plot_backend(backend=None):
16031601
The backend is imported lazily, as matplotlib is a soft dependency, and
16041602
pandas can be used without it being installed.
16051603
"""
1606-
backend = backend or pandas.get_option("plotting.backend")
1604+
backend = backend or get_option("plotting.backend")
16071605

16081606
if backend == "matplotlib":
16091607
# Because matplotlib is an optional dependency and first-party backend,

pandas/plotting/_matplotlib/converter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"):
329329

330330
class PandasAutoDateLocator(dates.AutoDateLocator):
331331
def get_locator(self, dmin, dmax):
332-
"Pick the best locator based on a distance."
332+
"""Pick the best locator based on a distance."""
333333
_check_implicitly_registered()
334334
delta = relativedelta(dmax, dmin)
335335

@@ -382,6 +382,7 @@ def __call__(self):
382382
dmax, dmin = dmin, dmax
383383
# We need to cap at the endpoints of valid datetime
384384

385+
# FIXME: dont leave commented-out
385386
# TODO(wesm) unused?
386387
# delta = relativedelta(dmax, dmin)
387388
# try:
@@ -448,6 +449,7 @@ def autoscale(self):
448449

449450
# We need to cap at the endpoints of valid datetime
450451

452+
# FIXME: dont leave commented-out
451453
# TODO(wesm): unused?
452454

453455
# delta = relativedelta(dmax, dmin)

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Optional # noqa
2+
from typing import Optional
33
import warnings
44

55
import numpy as np

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ def run(self):
300300
for clean_me in self._clean_me:
301301
try:
302302
os.unlink(clean_me)
303-
except Exception:
303+
except OSError:
304304
pass
305305
for clean_tree in self._clean_trees:
306306
try:
307307
shutil.rmtree(clean_tree)
308-
except Exception:
308+
except OSError:
309309
pass
310310

311311

0 commit comments

Comments
 (0)