Skip to content

REF: _infer_tsobject_fold to infer_datetuil_fold #46516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ cdef int64_t get_datetime64_nanos(object val) except? -1
cpdef datetime localize_pydatetime(datetime dt, tzinfo tz)
cdef int64_t cast_from_unit(object ts, str unit) except? -1
cpdef (int64_t, int) precision_from_unit(str unit)

cdef int64_t normalize_i8_stamp(int64_t local_val) nogil
69 changes: 3 additions & 66 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ from pandas._libs.tslibs.nattype cimport (
)
from pandas._libs.tslibs.tzconversion cimport (
bisect_right_i8,
infer_datetuil_fold,
localize_tzinfo_api,
tz_localize_to_utc_single,
)
Expand Down Expand Up @@ -530,7 +531,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts,
if typ == 'dateutil':
tdata = <int64_t*>cnp.PyArray_DATA(trans)
pos = bisect_right_i8(tdata, obj.value, trans.shape[0]) - 1
obj.fold = _infer_tsobject_fold(obj, trans, deltas, pos)
obj.fold = infer_datetuil_fold(obj.value, trans, deltas, pos)

# Keep the converter same as PyDateTime's
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
Expand Down Expand Up @@ -714,7 +715,7 @@ cdef inline void _localize_tso(_TSObject obj, tzinfo tz):
local_val = obj.value + deltas[pos]

# dateutil supports fold, so we infer fold from value
obj.fold = _infer_tsobject_fold(obj, trans, deltas, pos)
obj.fold = infer_datetuil_fold(obj.value, trans, deltas, pos)
else:
# All other cases have len(deltas) == 1. As of 2018-07-17
# (and 2022-03-07), all test cases that get here have
Expand All @@ -726,49 +727,6 @@ cdef inline void _localize_tso(_TSObject obj, tzinfo tz):
obj.tzinfo = tz


cdef inline bint _infer_tsobject_fold(
_TSObject obj,
const int64_t[:] trans,
const int64_t[:] deltas,
intp_t pos,
):
"""
Infer _TSObject fold property from value by assuming 0 and then setting
to 1 if necessary.

Parameters
----------
obj : _TSObject
trans : ndarray[int64_t]
ndarray of offset transition points in nanoseconds since epoch.
deltas : int64_t[:]
array of offsets corresponding to transition points in trans.
pos : intp_t
Position of the last transition point before taking fold into account.

Returns
-------
bint
Due to daylight saving time, one wall clock time can occur twice
when shifting from summer to winter time; fold describes whether the
datetime-like corresponds to the first (0) or the second time (1)
the wall clock hits the ambiguous time

References
----------
.. [1] "PEP 495 - Local Time Disambiguation"
https://www.python.org/dev/peps/pep-0495/#the-fold-attribute
"""
cdef:
bint fold = 0

if pos > 0:
fold_delta = deltas[pos - 1] - deltas[pos]
if obj.value - fold_delta < trans[pos]:
fold = 1

return fold

cdef inline datetime _localize_pydatetime(datetime dt, tzinfo tz):
"""
Take a datetime/Timestamp in UTC and localizes to timezone tz.
Expand Down Expand Up @@ -802,24 +760,3 @@ cpdef inline datetime localize_pydatetime(datetime dt, tzinfo tz):
elif isinstance(dt, ABCTimestamp):
return dt.tz_localize(tz)
return _localize_pydatetime(dt, tz)


# ----------------------------------------------------------------------
# Normalization

@cython.cdivision(False)
cdef inline int64_t normalize_i8_stamp(int64_t local_val) nogil:
"""
Round the localized nanosecond timestamp down to the previous midnight.

Parameters
----------
local_val : int64_t

Returns
-------
int64_t
"""
cdef:
int64_t day_nanos = 24 * 3600 * 1_000_000_000
return local_val - (local_val % day_nanos)
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/timestamps.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ cdef class _Timestamp(ABCTimestamp):
int op) except -1
cpdef void _set_freq(self, freq)
cdef _warn_on_field_deprecation(_Timestamp self, freq, str field)

cdef int64_t normalize_i8_stamp(int64_t local_val) nogil
21 changes: 20 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ from pandas._libs.tslibs.conversion cimport (
_TSObject,
convert_datetime_to_tsobject,
convert_to_tsobject,
normalize_i8_stamp,
)
from pandas._libs.tslibs.util cimport (
is_array,
Expand Down Expand Up @@ -2116,3 +2115,23 @@ cdef int64_t _NS_LOWER_BOUND = NPY_NAT + 1
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365


# ----------------------------------------------------------------------
# Scalar analogues to functions in vectorized.pyx


@cython.cdivision(False)
cdef inline int64_t normalize_i8_stamp(int64_t local_val) nogil:
"""
Round the localized nanosecond timestamp down to the previous midnight.

Parameters
----------
local_val : int64_t

Returns
-------
int64_t
"""
return local_val - (local_val % ccalendar.DAY_NANOS)
12 changes: 11 additions & 1 deletion pandas/_libs/tslibs/tzconversion.pxd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from cpython.datetime cimport tzinfo
from numpy cimport int64_t
from numpy cimport (
int64_t,
intp_t,
)


cdef int64_t localize_tzinfo_api(
Expand All @@ -11,3 +14,10 @@ cdef int64_t tz_localize_to_utc_single(
) except? -1

cdef Py_ssize_t bisect_right_i8(int64_t *data, int64_t val, Py_ssize_t n)

cdef bint infer_datetuil_fold(
int64_t value,
const int64_t[::1] trans,
const int64_t[::1] deltas,
intp_t pos,
)
45 changes: 45 additions & 0 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,48 @@ cdef int64_t _tz_localize_using_tzinfo_api(
td = tz.utcoffset(dt)
delta = int(td.total_seconds() * 1_000_000_000)
return delta


# NB: relies on dateutil internals, subject to change.
cdef bint infer_datetuil_fold(
int64_t value,
const int64_t[::1] trans,
const int64_t[::1] deltas,
intp_t pos,
):
"""
Infer _TSObject fold property from value by assuming 0 and then setting
to 1 if necessary.

Parameters
----------
value : int64_t
trans : ndarray[int64_t]
ndarray of offset transition points in nanoseconds since epoch.
deltas : int64_t[:]
array of offsets corresponding to transition points in trans.
pos : intp_t
Position of the last transition point before taking fold into account.

Returns
-------
bint
Due to daylight saving time, one wall clock time can occur twice
when shifting from summer to winter time; fold describes whether the
datetime-like corresponds to the first (0) or the second time (1)
the wall clock hits the ambiguous time

References
----------
.. [1] "PEP 495 - Local Time Disambiguation"
https://www.python.org/dev/peps/pep-0495/#the-fold-attribute
"""
cdef:
bint fold = 0

if pos > 0:
fold_delta = deltas[pos - 1] - deltas[pos]
if value - fold_delta < trans[pos]:
fold = 1

return fold
7 changes: 4 additions & 3 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ from numpy cimport (

cnp.import_array()

from .conversion cimport normalize_i8_stamp

from .dtypes import Resolution

from .ccalendar cimport DAY_NANOS
Expand All @@ -34,7 +32,10 @@ from .np_datetime cimport (
)
from .offsets cimport BaseOffset
from .period cimport get_period_ordinal
from .timestamps cimport create_timestamp_from_ts
from .timestamps cimport (
create_timestamp_from_ts,
normalize_i8_stamp,
)
from .timezones cimport (
get_dst_info,
is_tzlocal,
Expand Down