Skip to content

Commit 5d1440e

Browse files
jbrockmendeljreback
authored andcommitted
CLN: avoid bare except in tslib and tslibs.parsing (#28345)
1 parent 17f73aa commit 5d1440e

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pandas/_libs/tslib.pyx

+5-6
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,13 @@ def array_with_unit_to_datetime(ndarray values, object unit,
344344
# try a quick conversion to i8
345345
# if we have nulls that are not type-compat
346346
# then need to iterate
347-
try:
347+
if values.dtype.kind == "i":
348+
# Note: this condition makes the casting="same_kind" redundant
348349
iresult = values.astype('i8', casting='same_kind', copy=False)
349350
mask = iresult == NPY_NAT
350351
iresult[mask] = 0
351352
fvalues = iresult.astype('f8') * m
352353
need_to_iterate = False
353-
except:
354-
pass
355354

356355
# check the bounds
357356
if not need_to_iterate:
@@ -406,7 +405,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
406405
elif is_ignore:
407406
raise AssertionError
408407
iresult[i] = NPY_NAT
409-
except:
408+
except OverflowError:
410409
if is_raise:
411410
raise OutOfBoundsDatetime(
412411
"cannot convert input {val} with the unit "
@@ -447,7 +446,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
447446
else:
448447
try:
449448
oresult[i] = Timestamp(cast_from_unit(val, unit))
450-
except:
449+
except OverflowError:
451450
oresult[i] = val
452451

453452
elif isinstance(val, str):
@@ -574,7 +573,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
574573
# datetimes/strings, then we must coerce)
575574
try:
576575
iresult[i] = cast_from_unit(val, 'ns')
577-
except:
576+
except OverflowError:
578577
iresult[i] = NPY_NAT
579578

580579
elif isinstance(val, str):

pandas/_libs/tslibs/parsing.pyx

+10-17
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,11 @@ def try_parse_dates(object[:] values, parser=None,
587587
else:
588588
parse_date = parser
589589

590-
try:
591-
for i in range(n):
592-
if values[i] == '':
593-
result[i] = np.nan
594-
else:
595-
result[i] = parse_date(values[i])
596-
except Exception:
597-
# raise if passed parser and it failed
598-
raise
590+
for i in range(n):
591+
if values[i] == '':
592+
result[i] = np.nan
593+
else:
594+
result[i] = parse_date(values[i])
599595

600596
return result.base # .base to access underlying ndarray
601597

@@ -814,7 +810,7 @@ def _guess_datetime_format(dt_str, dayfirst=False, dt_str_parse=du_parse,
814810
if dt_str_parse is None or dt_str_split is None:
815811
return None
816812

817-
if not isinstance(dt_str, (str, unicode)):
813+
if not isinstance(dt_str, str):
818814
return None
819815

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

841837
try:
842838
parsed_datetime = dt_str_parse(dt_str, dayfirst=dayfirst)
843-
except:
839+
except (ValueError, OverflowError):
844840
# In case the datetime can't be parsed, its format cannot be guessed
845841
return None
846842

847843
if parsed_datetime is None:
848844
return None
849845

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

857850
format_guess = [None] * len(tokens)
858851
found_attrs = set()

0 commit comments

Comments
 (0)