Skip to content

Commit 5049c7f

Browse files
anmyachevjreback
authored andcommitted
speed up 'create_tsobject_tz_using_offset' func (#26334)
1 parent 8e62539 commit 5049c7f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

pandas/_libs/tslibs/conversion.pyx

+11-12
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,15 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz,
392392
return obj
393393

394394

395-
cdef _TSObject create_tsobject_tz_using_offset(int64_t value,
395+
cdef _TSObject create_tsobject_tz_using_offset(npy_datetimestruct dts,
396396
int tzoffset, object tz=None):
397397
"""
398-
Convert a numpy datetime64 `value`, along with initial timezone offset
398+
Convert a datetimestruct `dts`, along with initial timezone offset
399399
`tzoffset` to a _TSObject (with timezone object `tz` - optional).
400400
401401
Parameters
402402
----------
403-
value: int64_t
404-
numpy dt64
403+
dts: npy_datetimestruct
405404
tzoffset: int
406405
tz : tzinfo or None
407406
timezone for the timezone-aware output.
@@ -411,12 +410,14 @@ cdef _TSObject create_tsobject_tz_using_offset(int64_t value,
411410
obj : _TSObject
412411
"""
413412
cdef:
414-
_TSObject obj
413+
_TSObject obj = _TSObject()
414+
int64_t value # numpy dt64
415415
datetime dt
416416

417-
tzinfo = pytz.FixedOffset(tzoffset)
418-
value = tz_convert_single(value, tzinfo, UTC)
419-
obj = convert_to_tsobject(value, tzinfo, None, 0, 0)
417+
value = dtstruct_to_dt64(&dts)
418+
obj.dts = dts
419+
obj.tzinfo = pytz.FixedOffset(tzoffset)
420+
obj.value = tz_convert_single(value, obj.tzinfo, UTC)
420421
if tz is None:
421422
check_overflows(obj)
422423
return obj
@@ -459,7 +460,6 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
459460
"""
460461
cdef:
461462
npy_datetimestruct dts
462-
int64_t value # numpy dt64
463463
int out_local = 0, out_tzoffset = 0
464464
bint do_parse_datetime_string = False
465465

@@ -487,12 +487,11 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
487487
try:
488488
if not string_to_dts_failed:
489489
check_dts_bounds(&dts)
490-
value = dtstruct_to_dt64(&dts)
491490
if out_local == 1:
492-
return create_tsobject_tz_using_offset(value,
491+
return create_tsobject_tz_using_offset(dts,
493492
out_tzoffset, tz)
494493
else:
495-
ts = value
494+
ts = dtstruct_to_dt64(&dts)
496495
if tz is not None:
497496
# shift for localize_tso
498497
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,

0 commit comments

Comments
 (0)