Skip to content

Commit 47a45a8

Browse files
committed
REF: move exception handling within dateutil_parse
1 parent 82e4eb8 commit 47a45a8

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

pandas/_libs/tslibs/parsing.pyx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,10 @@ def parse_datetime_string(
286286
except ValueError:
287287
pass
288288

289-
try:
290-
dt, _ = dateutil_parse(date_string, default=_DEFAULT_DATETIME,
291-
dayfirst=dayfirst, yearfirst=yearfirst,
292-
ignoretz=False)
293-
except OverflowError as err:
294-
# with e.g. "08335394550" dateutil raises when trying to pass
295-
# year=8335394550 to datetime.replace
296-
raise OutOfBoundsDatetime(
297-
f'Parsing "{date_string}" to datetime overflows'
298-
) from err
289+
dt, _ = dateutil_parse(date_string, default=_DEFAULT_DATETIME,
290+
dayfirst=dayfirst, yearfirst=yearfirst,
291+
ignoretz=False)
292+
299293
if dt.tzinfo is not None:
300294
# dateutil can return a datetime with a tzoffset outside of (-24H, 24H)
301295
# bounds, which is invalid (can be constructed, but raises if we call
@@ -412,13 +406,9 @@ def parse_datetime_string_with_reso(
412406
except ValueError:
413407
pass
414408

415-
try:
416-
parsed, reso = dateutil_parse(date_string, _DEFAULT_DATETIME,
417-
dayfirst=dayfirst, yearfirst=yearfirst,
418-
ignoretz=False)
419-
except (ValueError, OverflowError) as err:
420-
# e.g. "day is out of range for month" raised in default.replace
421-
raise DateParseError(err) from err
409+
parsed, reso = dateutil_parse(date_string, _DEFAULT_DATETIME,
410+
dayfirst=dayfirst, yearfirst=yearfirst,
411+
ignoretz=False)
422412
return parsed, reso
423413

424414

@@ -618,7 +608,7 @@ cdef dateutil_parse(
618608
str attr
619609
datetime ret
620610
object res
621-
object reso = None
611+
str reso = None
622612
dict repl = {}
623613

624614
res, _ = DEFAULTPARSER._parse(timestr, dayfirst=dayfirst, yearfirst=yearfirst)
@@ -647,8 +637,15 @@ cdef dateutil_parse(
647637
try:
648638
ret = default.replace(**repl)
649639
except ValueError as err:
640+
# e.g. "day is out of range for month"
650641
# we re-raise to match dateutil's exception message
651-
raise ValueError(str(err) + ": " + timestr) from err
642+
raise DateParseError(str(err) + ": " + timestr) from err
643+
except OverflowError as err:
644+
# with e.g. "08335394550" dateutil raises when trying to pass
645+
# year=8335394550 to datetime.replace
646+
raise OutOfBoundsDatetime(
647+
f'Parsing "{timestr}" to datetime overflows'
648+
) from err
652649

653650
if res.weekday is not None and not res.day:
654651
ret = ret + relativedelta.relativedelta(weekday=res.weekday)

0 commit comments

Comments
 (0)