Skip to content

CLN: avoid bare except in tslib and tslibs.parsing #28345

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 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,13 @@ def array_with_unit_to_datetime(ndarray values, object unit,
# try a quick conversion to i8
# if we have nulls that are not type-compat
# then need to iterate
try:
if values.dtype.kind == "i":
# Note: this condition makes the casting="same_kind" redundant
iresult = values.astype('i8', casting='same_kind', copy=False)
mask = iresult == NPY_NAT
iresult[mask] = 0
fvalues = iresult.astype('f8') * m
need_to_iterate = False
except:
pass

# check the bounds
if not need_to_iterate:
Expand Down Expand Up @@ -406,7 +405,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
elif is_ignore:
raise AssertionError
iresult[i] = NPY_NAT
except:
except OverflowError:
if is_raise:
raise OutOfBoundsDatetime(
"cannot convert input {val} with the unit "
Expand Down Expand Up @@ -447,7 +446,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
else:
try:
oresult[i] = Timestamp(cast_from_unit(val, unit))
except:
except OverflowError:
oresult[i] = val

elif isinstance(val, str):
Expand Down Expand Up @@ -574,7 +573,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
# datetimes/strings, then we must coerce)
try:
iresult[i] = cast_from_unit(val, 'ns')
except:
except OverflowError:
iresult[i] = NPY_NAT

elif isinstance(val, str):
Expand Down
27 changes: 10 additions & 17 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,11 @@ def try_parse_dates(object[:] values, parser=None,
else:
parse_date = parser

try:
for i in range(n):
if values[i] == '':
result[i] = np.nan
else:
result[i] = parse_date(values[i])
except Exception:
# raise if passed parser and it failed
raise
for i in range(n):
if values[i] == '':
result[i] = np.nan
else:
result[i] = parse_date(values[i])

return result.base # .base to access underlying ndarray

Expand Down Expand Up @@ -814,7 +810,7 @@ def _guess_datetime_format(dt_str, dayfirst=False, dt_str_parse=du_parse,
if dt_str_parse is None or dt_str_split is None:
return None

if not isinstance(dt_str, (str, unicode)):
if not isinstance(dt_str, str):
return None

day_attribute_and_format = (('day',), '%d', 2)
Expand All @@ -840,19 +836,16 @@ def _guess_datetime_format(dt_str, dayfirst=False, dt_str_parse=du_parse,

try:
parsed_datetime = dt_str_parse(dt_str, dayfirst=dayfirst)
except:
except (ValueError, OverflowError):
# In case the datetime can't be parsed, its format cannot be guessed
return None

if parsed_datetime is None:
return None

try:
tokens = dt_str_split(dt_str)
except:
# In case the datetime string can't be split, its format cannot
# be guessed
return None
# the default dt_str_split from dateutil will never raise here; we assume
# that any user-provided function will not either.
tokens = dt_str_split(dt_str)

format_guess = [None] * len(tokens)
found_attrs = set()
Expand Down