Skip to content

Commit fc8be37

Browse files
authored
REF: support reso in remaining tslibs helpers (#48661)
* REF: support reso in remaining tslibs helpers * update setup.py
1 parent e8a4ce2 commit fc8be37

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

pandas/_libs/tslib.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ from pandas._libs.tslibs.np_datetime cimport (
3333
NPY_DATETIMEUNIT,
3434
NPY_FR_ns,
3535
check_dts_bounds,
36-
dtstruct_to_dt64,
3736
get_datetime64_value,
3837
npy_datetimestruct,
38+
npy_datetimestruct_to_datetime,
3939
pandas_datetime_to_datetimestruct,
4040
pydate_to_dt64,
4141
pydatetime_to_dt64,
@@ -95,7 +95,7 @@ def _test_parse_iso8601(ts: str):
9595
return Timestamp.now().normalize()
9696

9797
string_to_dts(ts, &obj.dts, &out_bestunit, &out_local, &out_tzoffset, True)
98-
obj.value = dtstruct_to_dt64(&obj.dts)
98+
obj.value = npy_datetimestruct_to_datetime(NPY_FR_ns, &obj.dts)
9999
check_dts_bounds(&obj.dts)
100100
if out_local == 1:
101101
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
@@ -547,7 +547,7 @@ cpdef array_to_datetime(
547547

548548
elif is_datetime64_object(val):
549549
seen_datetime = True
550-
iresult[i] = get_datetime64_nanos(val)
550+
iresult[i] = get_datetime64_nanos(val, NPY_FR_ns)
551551

552552
elif is_integer_object(val) or is_float_object(val):
553553
# these must be ns unit by-definition
@@ -628,7 +628,7 @@ cpdef array_to_datetime(
628628
if not string_to_dts_failed:
629629
# No error reported by string_to_dts, pick back up
630630
# where we left off
631-
value = dtstruct_to_dt64(&dts)
631+
value = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
632632
if out_local == 1:
633633
seen_datetime_offset = True
634634
# Store the out_tzoffset in seconds

pandas/_libs/tslibs/conversion.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, tzinfo tz,
3030
int32_t nanos=*,
3131
NPY_DATETIMEUNIT reso=*)
3232

33-
cdef int64_t get_datetime64_nanos(object val) except? -1
33+
cdef int64_t get_datetime64_nanos(object val, NPY_DATETIMEUNIT reso) except? -1
3434

3535
cpdef datetime localize_pydatetime(datetime dt, tzinfo tz)
3636
cdef int64_t cast_from_unit(object ts, str unit) except? -1

pandas/_libs/tslibs/conversion.pyx

+8-9
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ from pandas._libs.tslibs.np_datetime cimport (
3737
NPY_DATETIMEUNIT,
3838
NPY_FR_ns,
3939
check_dts_bounds,
40-
dtstruct_to_dt64,
4140
get_datetime64_unit,
4241
get_datetime64_value,
4342
get_implementation_bounds,
@@ -171,7 +170,7 @@ cpdef inline (int64_t, int) precision_from_unit(str unit):
171170
return m, p
172171

173172

174-
cdef inline int64_t get_datetime64_nanos(object val) except? -1:
173+
cdef inline int64_t get_datetime64_nanos(object val, NPY_DATETIMEUNIT reso) except? -1:
175174
"""
176175
Extract the value and unit from a np.datetime64 object, then convert the
177176
value to nanoseconds if necessary.
@@ -187,10 +186,10 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
187186

188187
unit = get_datetime64_unit(val)
189188

190-
if unit != NPY_FR_ns:
189+
if unit != reso:
191190
pandas_datetime_to_datetimestruct(ival, unit, &dts)
192-
check_dts_bounds(&dts)
193-
ival = dtstruct_to_dt64(&dts)
191+
check_dts_bounds(&dts, reso)
192+
ival = npy_datetimestruct_to_datetime(reso, &dts)
194193

195194
return ival
196195

@@ -238,7 +237,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
238237
if ts is None or ts is NaT:
239238
obj.value = NPY_NAT
240239
elif is_datetime64_object(ts):
241-
obj.value = get_datetime64_nanos(ts)
240+
obj.value = get_datetime64_nanos(ts, NPY_FR_ns)
242241
if obj.value != NPY_NAT:
243242
pandas_datetime_to_datetimestruct(obj.value, NPY_FR_ns, &obj.dts)
244243
elif is_integer_object(ts):
@@ -403,7 +402,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts,
403402
datetime dt
404403
Py_ssize_t pos
405404

406-
value = dtstruct_to_dt64(&dts)
405+
value = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
407406
obj.dts = dts
408407
obj.tzinfo = pytz.FixedOffset(tzoffset)
409408
obj.value = tz_localize_to_utc_single(value, obj.tzinfo)
@@ -490,12 +489,12 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit,
490489
)
491490
if not string_to_dts_failed:
492491
try:
493-
check_dts_bounds(&dts)
492+
check_dts_bounds(&dts, NPY_FR_ns)
494493
if out_local == 1:
495494
return _create_tsobject_tz_using_offset(dts,
496495
out_tzoffset, tz)
497496
else:
498-
ival = dtstruct_to_dt64(&dts)
497+
ival = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
499498
if tz is not None:
500499
# shift for _localize_tso
501500
ival = tz_localize_to_utc_single(ival, tz,

pandas/_libs/tslibs/np_datetime.pxd

+6-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ cdef bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1
7575

7676
cdef check_dts_bounds(npy_datetimestruct *dts, NPY_DATETIMEUNIT unit=?)
7777

78-
cdef int64_t dtstruct_to_dt64(npy_datetimestruct* dts) nogil
79-
80-
cdef int64_t pydatetime_to_dt64(datetime val, npy_datetimestruct *dts)
78+
cdef int64_t pydatetime_to_dt64(
79+
datetime val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
80+
)
8181
cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts)
82-
cdef int64_t pydate_to_dt64(date val, npy_datetimestruct *dts)
82+
cdef int64_t pydate_to_dt64(
83+
date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
84+
)
8385
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts)
8486

8587
cdef npy_datetime get_datetime64_value(object obj) nogil

pandas/_libs/tslibs/np_datetime.pyx

+7-9
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ cdef check_dts_bounds(npy_datetimestruct *dts, NPY_DATETIMEUNIT unit=NPY_FR_ns):
215215
# ----------------------------------------------------------------------
216216
# Conversion
217217

218-
cdef inline int64_t dtstruct_to_dt64(npy_datetimestruct* dts) nogil:
219-
"""Convenience function to call npy_datetimestruct_to_datetime
220-
with the by-far-most-common frequency NPY_FR_ns"""
221-
return npy_datetimestruct_to_datetime(NPY_FR_ns, dts)
222-
223218

224219
# just exposed for testing at the moment
225220
def py_td64_to_tdstruct(int64_t td64, NPY_DATETIMEUNIT unit):
@@ -247,12 +242,13 @@ cdef inline void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts):
247242

248243

249244
cdef inline int64_t pydatetime_to_dt64(datetime val,
250-
npy_datetimestruct *dts):
245+
npy_datetimestruct *dts,
246+
NPY_DATETIMEUNIT reso=NPY_FR_ns):
251247
"""
252248
Note we are assuming that the datetime object is timezone-naive.
253249
"""
254250
pydatetime_to_dtstruct(val, dts)
255-
return dtstruct_to_dt64(dts)
251+
return npy_datetimestruct_to_datetime(reso, dts)
256252

257253

258254
cdef inline void pydate_to_dtstruct(date val, npy_datetimestruct *dts):
@@ -263,9 +259,11 @@ cdef inline void pydate_to_dtstruct(date val, npy_datetimestruct *dts):
263259
dts.ps = dts.as = 0
264260
return
265261

266-
cdef inline int64_t pydate_to_dt64(date val, npy_datetimestruct *dts):
262+
cdef inline int64_t pydate_to_dt64(
263+
date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=NPY_FR_ns
264+
):
267265
pydate_to_dtstruct(val, dts)
268-
return dtstruct_to_dt64(dts)
266+
return npy_datetimestruct_to_datetime(reso, dts)
269267

270268

271269
cdef inline int string_to_dts(

pandas/_libs/tslibs/strptime.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ from pandas._libs.tslibs.nattype cimport (
2121
c_nat_strings as nat_strings,
2222
)
2323
from pandas._libs.tslibs.np_datetime cimport (
24+
NPY_FR_ns,
2425
check_dts_bounds,
25-
dtstruct_to_dt64,
2626
npy_datetimestruct,
27+
npy_datetimestruct_to_datetime,
2728
)
2829

2930

@@ -329,7 +330,7 @@ def array_strptime(ndarray[object] values, str fmt, bint exact=True, errors='rai
329330
dts.us = us
330331
dts.ps = ns * 1000
331332

332-
iresult[i] = dtstruct_to_dt64(&dts)
333+
iresult[i] = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
333334
try:
334335
check_dts_bounds(&dts)
335336
except ValueError:

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
538538
"_libs.tslibs.strptime": {
539539
"pyxfile": "_libs/tslibs/strptime",
540540
"depends": tseries_depends,
541+
"sources": ["pandas/_libs/tslibs/src/datetime/np_datetime.c"],
541542
},
542543
"_libs.tslibs.timedeltas": {
543544
"pyxfile": "_libs/tslibs/timedeltas",

0 commit comments

Comments
 (0)