Skip to content

Commit bec5b3f

Browse files
committed
removed extra 'check_dts_bounds' call and 'try/except Exception:' block
1 parent 9e0c09d commit bec5b3f

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

pandas/_libs/tslibs/conversion.pyx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
424424
_TSObject obj
425425
int out_local = 0, out_tzoffset = 0
426426
datetime dt
427+
bint do_parse_datetime_string = False
427428

428429
if tz is not None:
429430
tz = maybe_get_tz(tz)
@@ -448,21 +449,14 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
448449
ts, &obj.dts, &out_local,
449450
&out_tzoffset, False
450451
)
451-
if string_to_dts_failed:
452-
try:
453-
ts = parse_datetime_string(ts, dayfirst=dayfirst,
454-
yearfirst=yearfirst)
455-
except Exception:
456-
raise ValueError("could not convert string to Timestamp")
457-
else:
458-
try:
452+
try:
453+
if not string_to_dts_failed:
459454
obj.value = dtstruct_to_dt64(&obj.dts)
460455
check_dts_bounds(&obj.dts)
461456
if out_local == 1:
462457
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
463458
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
464459
if tz is None:
465-
check_dts_bounds(&obj.dts)
466460
check_overflows(obj)
467461
return obj
468462
else:
@@ -483,13 +477,17 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
483477
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
484478
ambiguous='raise')[0]
485479

486-
except OutOfBoundsDatetime:
487-
# GH#19382 for just-barely-OutOfBounds falling back to dateutil
488-
# parser will return incorrect result because it will ignore
489-
# nanoseconds
490-
raise
480+
except OutOfBoundsDatetime:
481+
# GH#19382 for just-barely-OutOfBounds falling back to dateutil
482+
# parser will return incorrect result because it will ignore
483+
# nanoseconds
484+
raise
485+
486+
except ValueError:
487+
do_parse_datetime_string = True
491488

492-
except ValueError:
489+
finally:
490+
if string_to_dts_failed or do_parse_datetime_string:
493491
try:
494492
ts = parse_datetime_string(ts, dayfirst=dayfirst,
495493
yearfirst=yearfirst)

0 commit comments

Comments
 (0)