@@ -423,6 +423,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
423
423
_TSObject obj
424
424
int out_local = 0 , out_tzoffset = 0
425
425
datetime dt
426
+ bint do_parse_datetime_string = False
426
427
427
428
if tz is not None :
428
429
tz = maybe_get_tz(tz)
@@ -447,21 +448,14 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
447
448
ts, & obj.dts, & out_local,
448
449
& out_tzoffset, False
449
450
)
450
- if string_to_dts_failed:
451
- try :
452
- ts = parse_datetime_string(ts, dayfirst = dayfirst,
453
- yearfirst = yearfirst)
454
- except Exception :
455
- raise ValueError (" could not convert string to Timestamp" )
456
- else :
457
- try :
451
+ try :
452
+ if not string_to_dts_failed:
458
453
obj.value = dtstruct_to_dt64(& obj.dts)
459
454
check_dts_bounds(& obj.dts)
460
455
if out_local == 1 :
461
456
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
462
457
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
463
458
if tz is None :
464
- check_dts_bounds(& obj.dts)
465
459
check_overflows(obj)
466
460
return obj
467
461
else :
@@ -482,13 +476,17 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
482
476
ts = tz_localize_to_utc(np.array([ts], dtype = ' i8' ), tz,
483
477
ambiguous = ' raise' )[0 ]
484
478
485
- except OutOfBoundsDatetime:
486
- # GH#19382 for just-barely-OutOfBounds falling back to dateutil
487
- # parser will return incorrect result because it will ignore
488
- # nanoseconds
489
- raise
479
+ except OutOfBoundsDatetime:
480
+ # GH#19382 for just-barely-OutOfBounds falling back to dateutil
481
+ # parser will return incorrect result because it will ignore
482
+ # nanoseconds
483
+ raise
484
+
485
+ except ValueError :
486
+ do_parse_datetime_string = True
490
487
491
- except ValueError :
488
+ finally :
489
+ if string_to_dts_failed or do_parse_datetime_string:
492
490
try :
493
491
ts = parse_datetime_string(ts, dayfirst = dayfirst,
494
492
yearfirst = yearfirst)
0 commit comments