@@ -520,43 +520,35 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit,
520
520
)
521
521
if not string_to_dts_failed:
522
522
reso = get_supported_reso(out_bestunit)
523
- try :
524
- check_dts_bounds(& dts, reso)
525
- if out_local == 1 :
526
- return _create_tsobject_tz_using_offset(
527
- dts, out_tzoffset, tz, reso
523
+ check_dts_bounds(& dts, reso)
524
+ if out_local == 1 :
525
+ return _create_tsobject_tz_using_offset(
526
+ dts, out_tzoffset, tz, reso
527
+ )
528
+ else :
529
+ ival = npy_datetimestruct_to_datetime(reso, & dts)
530
+ if tz is not None :
531
+ # shift for _localize_tso
532
+ ival = tz_localize_to_utc_single(
533
+ ival, tz, ambiguous = " raise" , nonexistent = None , creso = reso
528
534
)
529
- else :
530
- ival = npy_datetimestruct_to_datetime(reso, & dts)
531
- if tz is not None :
532
- # shift for _localize_tso
533
- ival = tz_localize_to_utc_single(
534
- ival, tz, ambiguous = " raise" , nonexistent = None , creso = reso
535
- )
536
- obj = _TSObject()
537
- obj.dts = dts
538
- obj.value = ival
539
- obj.creso = reso
540
- maybe_localize_tso(obj, tz, obj.creso)
541
- return obj
542
-
543
- except OutOfBoundsDatetime:
544
- # GH#19382 for just-barely-OutOfBounds falling back to dateutil
545
- # parser will return incorrect result because it will ignore
546
- # nanoseconds
547
- raise
548
-
549
- except ValueError :
550
- # Fall through to parse_datetime_string
551
- pass
535
+ obj = _TSObject()
536
+ obj.dts = dts
537
+ obj.value = ival
538
+ obj.creso = reso
539
+ maybe_localize_tso(obj, tz, obj.creso)
540
+ return obj
552
541
553
542
try :
554
- # TODO: use the one that returns reso
555
543
dt = parse_datetime_string(
556
544
ts, dayfirst = dayfirst, yearfirst = yearfirst
557
545
)
558
- except (ValueError , OverflowError ) as err:
559
- raise ValueError (" could not convert string to Timestamp" ) from err
546
+ except ValueError as err:
547
+ if " out of range for month" in str (err):
548
+ # dateutil raised when constructing a datetime object,
549
+ # let's give a nicer exception message
550
+ raise ValueError (" could not convert string to Timestamp" ) from err
551
+ raise
560
552
561
553
return convert_datetime_to_tsobject(dt, tz)
562
554
0 commit comments