Skip to content

CLN: catch Exception less #28309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I guess this replicates existing behavior but is this really right? Seems counter intuitive to return True in a function called is_nonempty if the axis doesn't exist at all

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment just above the function "1-d dtypes always are included here" seems to address that. i agree it could be clearer, but I'm not going to lose sleep over it. ideas for better wording?

return True
return x.shape[axis] > 0

# If all arrays are empty, there's nothing to convert, just short-cut to
# the concatenation, #3121.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,9 +1114,6 @@ def __getitem__(self, key):
return self.__getitem__(new_key)
raise

except Exception:
raise

if is_iterator(key):
key = list(key)

Expand Down
10 changes: 5 additions & 5 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Optional # noqa
from typing import Optional # noqa:F401
import warnings

import numpy as np
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down