@@ -424,6 +424,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
424
424
_TSObject obj
425
425
int out_local = 0 , out_tzoffset = 0
426
426
datetime dt
427
+ bint do_parse_datetime_string = False
427
428
428
429
if tz is not None :
429
430
tz = maybe_get_tz(tz)
@@ -448,21 +449,14 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
448
449
ts, & obj.dts, & out_local,
449
450
& out_tzoffset, False
450
451
)
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:
459
454
obj.value = dtstruct_to_dt64(& obj.dts)
460
455
check_dts_bounds(& obj.dts)
461
456
if out_local == 1 :
462
457
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
463
458
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
464
459
if tz is None :
465
- check_dts_bounds(& obj.dts)
466
460
check_overflows(obj)
467
461
return obj
468
462
else :
@@ -483,13 +477,17 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
483
477
ts = tz_localize_to_utc(np.array([ts], dtype = ' i8' ), tz,
484
478
ambiguous = ' raise' )[0 ]
485
479
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
491
488
492
- except ValueError :
489
+ finally :
490
+ if string_to_dts_failed or do_parse_datetime_string:
493
491
try :
494
492
ts = parse_datetime_string(ts, dayfirst = dayfirst,
495
493
yearfirst = yearfirst)
0 commit comments