diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 01e500a80dcc4..dc06a30004d19 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -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: @@ -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 " @@ -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): @@ -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): diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index eb99f090e8565..3da3d1e4b1b41 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -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 @@ -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) @@ -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()