Skip to content

Commit 43cc10b

Browse files
committed
added new helper function - 'setup_tsobject_tz_using_offset'
1 parent 09115c6 commit 43cc10b

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

pandas/_libs/tslibs/conversion.pyx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz,
392392
return obj
393393

394394

395+
cdef _TSObject setup_tsobject_tz_using_offset(_TSObject obj,
396+
object tz, int tzoffset):
397+
obj.tzinfo = pytz.FixedOffset(tzoffset)
398+
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
399+
if tz is None:
400+
check_overflows(obj)
401+
return obj
402+
else:
403+
# Keep the converter same as PyDateTime's
404+
obj = convert_to_tsobject(obj.value, obj.tzinfo,
405+
None, 0, 0)
406+
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
407+
obj.dts.hour, obj.dts.min, obj.dts.sec,
408+
obj.dts.us, obj.tzinfo)
409+
obj = convert_datetime_to_tsobject(
410+
dt, tz, nanos=obj.dts.ps // 1000)
411+
return obj
412+
413+
395414
cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
396415
bint dayfirst=False,
397416
bint yearfirst=False):
@@ -450,25 +469,11 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
450469
)
451470
try:
452471
if not string_to_dts_failed:
453-
obj.value = dtstruct_to_dt64(&obj.dts)
454472
check_dts_bounds(&obj.dts)
473+
obj.value = dtstruct_to_dt64(&obj.dts)
455474
if out_local == 1:
456-
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
457-
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
458-
if tz is None:
459-
check_overflows(obj)
460-
return obj
461-
else:
462-
# Keep the converter same as PyDateTime's
463-
obj = convert_to_tsobject(obj.value, obj.tzinfo,
464-
None, 0, 0)
465-
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
466-
obj.dts.hour, obj.dts.min, obj.dts.sec,
467-
obj.dts.us, obj.tzinfo)
468-
obj = convert_datetime_to_tsobject(
469-
dt, tz, nanos=obj.dts.ps // 1000)
470-
return obj
471-
475+
return setup_tsobject_tz_using_offset(obj, tz,
476+
out_tzoffset)
472477
else:
473478
ts = obj.value
474479
if tz is not None:
@@ -485,13 +490,12 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
485490
except ValueError:
486491
do_parse_datetime_string = True
487492

488-
finally:
489-
if string_to_dts_failed or do_parse_datetime_string:
490-
try:
491-
ts = parse_datetime_string(ts, dayfirst=dayfirst,
492-
yearfirst=yearfirst)
493-
except Exception:
494-
raise ValueError("could not convert string to Timestamp")
493+
if string_to_dts_failed or do_parse_datetime_string:
494+
try:
495+
ts = parse_datetime_string(ts, dayfirst=dayfirst,
496+
yearfirst=yearfirst)
497+
except Exception:
498+
raise ValueError("could not convert string to Timestamp")
495499

496500
return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
497501

0 commit comments

Comments
 (0)