Skip to content

Commit 6010141

Browse files
authored
PERF: normalize_i8_timestamps (#34672)
1 parent d584cc1 commit 6010141

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

pandas/_libs/tslibs/conversion.pyx

+15-20
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,6 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
785785
int64_t[:] deltas
786786
str typ
787787
Py_ssize_t[:] pos
788-
npy_datetimestruct dts
789788
int64_t delta, local_val
790789

791790
if tz is None or is_utc(tz):
@@ -795,16 +794,14 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
795794
result[i] = NPY_NAT
796795
continue
797796
local_val = stamps[i]
798-
dt64_to_dtstruct(local_val, &dts)
799-
result[i] = _normalized_stamp(&dts)
797+
result[i] = _normalize_i8_stamp(local_val)
800798
elif is_tzlocal(tz):
801799
for i in range(n):
802800
if stamps[i] == NPY_NAT:
803801
result[i] = NPY_NAT
804802
continue
805803
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
806-
dt64_to_dtstruct(local_val, &dts)
807-
result[i] = _normalized_stamp(&dts)
804+
result[i] = _normalize_i8_stamp(local_val)
808805
else:
809806
# Adjust datetime64 timestamp, recompute datetimestruct
810807
trans, deltas, typ = get_dst_info(tz)
@@ -816,38 +813,36 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
816813
if stamps[i] == NPY_NAT:
817814
result[i] = NPY_NAT
818815
continue
819-
dt64_to_dtstruct(stamps[i] + delta, &dts)
820-
result[i] = _normalized_stamp(&dts)
816+
local_val = stamps[i] + delta
817+
result[i] = _normalize_i8_stamp(local_val)
821818
else:
822819
pos = trans.searchsorted(stamps, side='right') - 1
823820
for i in range(n):
824821
if stamps[i] == NPY_NAT:
825822
result[i] = NPY_NAT
826823
continue
827-
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
828-
result[i] = _normalized_stamp(&dts)
824+
local_val = stamps[i] + deltas[pos[i]]
825+
result[i] = _normalize_i8_stamp(local_val)
829826

830827
return result.base # `.base` to access underlying ndarray
831828

832829

833-
cdef inline int64_t _normalized_stamp(npy_datetimestruct *dts) nogil:
830+
@cython.cdivision
831+
cdef inline int64_t _normalize_i8_stamp(int64_t local_val) nogil:
834832
"""
835-
Normalize the given datetimestruct to midnight, then convert to int64_t.
833+
Round the localized nanosecond timestamp down to the previous midnight.
836834
837835
Parameters
838836
----------
839-
*dts : pointer to npy_datetimestruct
837+
local_val : int64_t
840838
841839
Returns
842840
-------
843-
stamp : int64
844-
"""
845-
dts.hour = 0
846-
dts.min = 0
847-
dts.sec = 0
848-
dts.us = 0
849-
dts.ps = 0
850-
return dtstruct_to_dt64(dts)
841+
int64_t
842+
"""
843+
cdef:
844+
int64_t day_nanos = 24 * 3600 * 1_000_000_000
845+
return local_val - (local_val % day_nanos)
851846

852847

853848
@cython.wraparound(False)

0 commit comments

Comments
 (0)