Skip to content

Commit fde7ec7

Browse files
authored
REF: make normalize_i8_timestamps cpdef (#34476)
1 parent f8ce4ca commit fde7ec7

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

pandas/_libs/tslibs/conversion.pxd

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from cpython.datetime cimport datetime
1+
from cpython.datetime cimport datetime, tzinfo
22

3-
from numpy cimport int64_t, int32_t
3+
from numpy cimport int64_t, int32_t, ndarray
44

55
from pandas._libs.tslibs.np_datetime cimport npy_datetimestruct
66

@@ -24,3 +24,5 @@ cdef int64_t get_datetime64_nanos(object val) except? -1
2424

2525
cpdef datetime localize_pydatetime(datetime dt, object tz)
2626
cdef int64_t cast_from_unit(object ts, str unit) except? -1
27+
28+
cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo tz)

pandas/_libs/tslibs/conversion.pyx

+2-27
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz):
763763

764764
@cython.wraparound(False)
765765
@cython.boundscheck(False)
766-
def normalize_i8_timestamps(int64_t[:] stamps, object tz):
766+
cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo tz):
767767
"""
768768
Normalize each of the (nanosecond) timezone aware timestamps in the given
769769
array by rounding down to the beginning of the day (i.e. midnight).
@@ -774,31 +774,6 @@ def normalize_i8_timestamps(int64_t[:] stamps, object tz):
774774
stamps : int64 ndarray
775775
tz : tzinfo or None
776776
777-
Returns
778-
-------
779-
result : int64 ndarray of converted of normalized nanosecond timestamps
780-
"""
781-
cdef:
782-
int64_t[:] result
783-
784-
result = _normalize_local(stamps, tz)
785-
786-
return result.base # .base to access underlying np.ndarray
787-
788-
789-
@cython.wraparound(False)
790-
@cython.boundscheck(False)
791-
cdef int64_t[:] _normalize_local(const int64_t[:] stamps, tzinfo tz):
792-
"""
793-
Normalize each of the (nanosecond) timestamps in the given array by
794-
rounding down to the beginning of the day (i.e. midnight) for the
795-
given timezone `tz`.
796-
797-
Parameters
798-
----------
799-
stamps : int64 ndarray
800-
tz : tzinfo
801-
802777
Returns
803778
-------
804779
result : int64 ndarray of converted of normalized nanosecond timestamps
@@ -843,7 +818,7 @@ cdef int64_t[:] _normalize_local(const int64_t[:] stamps, tzinfo tz):
843818
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
844819
result[i] = _normalized_stamp(&dts)
845820

846-
return result
821+
return result.base # `.base` to access underlying ndarray
847822

848823

849824
cdef inline int64_t _normalized_stamp(npy_datetimestruct *dts) nogil:

pandas/_libs/tslibs/timestamps.pyx

+24-10
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ cnp.import_array()
1616
from cpython.object cimport (PyObject_RichCompareBool, PyObject_RichCompare,
1717
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE)
1818

19-
from cpython.datetime cimport (datetime, time, PyDateTime_Check, PyDelta_Check,
20-
PyTZInfo_Check, PyDateTime_IMPORT)
19+
from cpython.datetime cimport (
20+
datetime,
21+
time,
22+
tzinfo,
23+
PyDateTime_Check,
24+
PyDelta_Check,
25+
PyTZInfo_Check,
26+
PyDateTime_IMPORT,
27+
)
2128
PyDateTime_IMPORT
2229

2330
from pandas._libs.tslibs.util cimport (
@@ -29,10 +36,12 @@ from pandas._libs.tslibs.base cimport ABCTimestamp
2936

3037
from pandas._libs.tslibs cimport ccalendar
3138

32-
from pandas._libs.tslibs.conversion import normalize_i8_timestamps
3339
from pandas._libs.tslibs.conversion cimport (
34-
_TSObject, convert_to_tsobject,
35-
convert_datetime_to_tsobject)
40+
_TSObject,
41+
convert_to_tsobject,
42+
convert_datetime_to_tsobject,
43+
normalize_i8_timestamps,
44+
)
3645
from pandas._libs.tslibs.fields import get_start_end_field, get_date_name_field
3746
from pandas._libs.tslibs.nattype cimport NPY_NAT, c_NaT as NaT
3847
from pandas._libs.tslibs.np_datetime cimport (
@@ -1438,13 +1447,18 @@ default 'raise'
14381447
"""
14391448
Normalize Timestamp to midnight, preserving tz information.
14401449
"""
1441-
if self.tz is None or is_utc(self.tz):
1450+
cdef:
1451+
ndarray[int64_t] normalized
1452+
tzinfo own_tz = self.tzinfo # could be None
1453+
1454+
if own_tz is None or is_utc(own_tz):
14421455
DAY_NS = ccalendar.DAY_NANOS
14431456
normalized_value = self.value - (self.value % DAY_NS)
1444-
return Timestamp(normalized_value).tz_localize(self.tz)
1445-
normalized_value = normalize_i8_timestamps(
1446-
np.array([self.value], dtype='i8'), tz=self.tz)[0]
1447-
return Timestamp(normalized_value).tz_localize(self.tz)
1457+
return Timestamp(normalized_value).tz_localize(own_tz)
1458+
1459+
normalized = normalize_i8_timestamps(
1460+
np.array([self.value], dtype='i8'), tz=own_tz)
1461+
return Timestamp(normalized[0]).tz_localize(own_tz)
14481462

14491463

14501464
# Add the min and max fields at the class level

0 commit comments

Comments
 (0)