@@ -581,55 +581,62 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit,
581581 """
582582 cdef:
583583 npy_datetimestruct dts
584- int out_local = 0 , out_tzoffset = 0
585- bint do_parse_datetime_string = False
584+ int out_local = 0 , out_tzoffset = 0 , string_to_dts_failed
585+ datetime dt
586+ int64_t ival
586587
587588 if len (ts) == 0 or ts in nat_strings:
588589 ts = NaT
590+ obj = _TSObject()
591+ obj.value = NPY_NAT
592+ obj.tzinfo = tz
593+ return obj
589594 elif ts == ' now' :
590595 # Issue 9000, we short-circuit rather than going
591596 # into np_datetime_strings which returns utc
592- ts = datetime.now(tz)
597+ dt = datetime.now(tz)
593598 elif ts == ' today' :
594599 # Issue 9000, we short-circuit rather than going
595600 # into np_datetime_strings which returns a normalized datetime
596- ts = datetime.now(tz)
601+ dt = datetime.now(tz)
597602 # equiv: datetime.today().replace(tzinfo=tz)
598603 else :
599604 string_to_dts_failed = _string_to_dts(
600605 ts, & dts, & out_local,
601606 & out_tzoffset, False
602607 )
603- try :
604- if not string_to_dts_failed :
608+ if not string_to_dts_failed :
609+ try :
605610 check_dts_bounds(& dts)
606611 if out_local == 1 :
607612 return _create_tsobject_tz_using_offset(dts,
608613 out_tzoffset, tz)
609614 else :
610- ts = dtstruct_to_dt64(& dts)
615+ ival = dtstruct_to_dt64(& dts)
611616 if tz is not None :
612617 # shift for _localize_tso
613- ts = tz_localize_to_utc_single(ts , tz,
614- ambiguous = " raise" )
618+ ival = tz_localize_to_utc_single(ival , tz,
619+ ambiguous = " raise" )
615620
616- except OutOfBoundsDatetime:
617- # GH#19382 for just-barely-OutOfBounds falling back to dateutil
618- # parser will return incorrect result because it will ignore
619- # nanoseconds
620- raise
621+ return convert_to_tsobject(ival, tz, None , False , False )
621622
622- except ValueError :
623- do_parse_datetime_string = True
623+ except OutOfBoundsDatetime:
624+ # GH#19382 for just-barely-OutOfBounds falling back to dateutil
625+ # parser will return incorrect result because it will ignore
626+ # nanoseconds
627+ raise
624628
625- if string_to_dts_failed or do_parse_datetime_string:
626- try :
627- ts = parse_datetime_string(ts, dayfirst = dayfirst,
628- yearfirst = yearfirst)
629- except (ValueError , OverflowError ):
630- raise ValueError (" could not convert string to Timestamp" )
629+ except ValueError :
630+ # Fall through to parse_datetime_string
631+ pass
632+
633+ try :
634+ dt = parse_datetime_string(ts, dayfirst = dayfirst,
635+ yearfirst = yearfirst)
636+ except (ValueError , OverflowError ):
637+ raise ValueError (" could not convert string to Timestamp" )
631638
632- return convert_to_tsobject(ts , tz, unit, dayfirst, yearfirst )
639+ return convert_datetime_to_tsobject(dt , tz)
633640
634641
635642cdef inline check_overflows(_TSObject obj):
@@ -688,12 +695,8 @@ cdef inline void _localize_tso(_TSObject obj, tzinfo tz):
688695 Sets obj.tzinfo inplace, alters obj.dts inplace.
689696 """
690697 cdef:
691- ndarray[int64_t] trans
692- int64_t[::1 ] deltas
693698 int64_t local_val
694- int64_t* tdata
695- Py_ssize_t pos, ntrans, outpos = - 1
696- str typ
699+ Py_ssize_t outpos = - 1
697700
698701 assert obj.tzinfo is None
699702
0 commit comments