Skip to content

Commit 4af9a8b

Browse files
jbrockmendeljreback
authored andcommitted
use np_datetime where possible (#18023)
1 parent a355ed2 commit 4af9a8b

File tree

4 files changed

+67
-113
lines changed

4 files changed

+67
-113
lines changed

pandas/_libs/period.pyx

+16-27
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ from pandas.compat import PY2
1717

1818
cimport cython
1919

20-
from datetime cimport (
21-
is_leapyear,
22-
pandas_datetimestruct,
23-
pandas_datetimestruct_to_datetime,
24-
pandas_datetime_to_datetimestruct,
25-
PANDAS_FR_ns)
20+
from tslibs.np_datetime cimport (pandas_datetimestruct,
21+
dtstruct_to_dt64, dt64_to_dtstruct)
22+
from datetime cimport is_leapyear
2623

2724

2825
cimport util
@@ -137,7 +134,7 @@ def dt64arr_to_periodarr(ndarray[int64_t] dtarr, int freq, tz=None):
137134
if dtarr[i] == NPY_NAT:
138135
out[i] = NPY_NAT
139136
continue
140-
pandas_datetime_to_datetimestruct(dtarr[i], PANDAS_FR_ns, &dts)
137+
dt64_to_dtstruct(dtarr[i], &dts)
141138
out[i] = get_period_ordinal(dts.year, dts.month, dts.day,
142139
dts.hour, dts.min, dts.sec,
143140
dts.us, dts.ps, freq)
@@ -268,7 +265,7 @@ cpdef int64_t period_ordinal_to_dt64(int64_t ordinal, int freq) nogil:
268265
dts.us = int((subsecond_fraction) * 1e6)
269266
dts.ps = int(((subsecond_fraction) * 1e6 - dts.us) * 1e6)
270267

271-
return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
268+
return dtstruct_to_dt64(&dts)
272269

273270

274271
def period_format(int64_t value, int freq, object fmt=None):
@@ -499,7 +496,7 @@ cpdef resolution(ndarray[int64_t] stamps, tz=None):
499496
for i in range(n):
500497
if stamps[i] == NPY_NAT:
501498
continue
502-
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
499+
dt64_to_dtstruct(stamps[i], &dts)
503500
curr_reso = _reso_stamp(&dts)
504501
if curr_reso < reso:
505502
reso = curr_reso
@@ -530,21 +527,19 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
530527
for i in range(n):
531528
if stamps[i] == NPY_NAT:
532529
continue
533-
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
530+
dt64_to_dtstruct(stamps[i], &dts)
534531
curr_reso = _reso_stamp(&dts)
535532
if curr_reso < reso:
536533
reso = curr_reso
537534
elif is_tzlocal(tz):
538535
for i in range(n):
539536
if stamps[i] == NPY_NAT:
540537
continue
541-
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns,
542-
&dts)
538+
dt64_to_dtstruct(stamps[i], &dts)
543539
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
544540
dts.min, dts.sec, dts.us, tz)
545541
delta = int(get_utcoffset(tz, dt).total_seconds()) * 1000000000
546-
pandas_datetime_to_datetimestruct(stamps[i] + delta,
547-
PANDAS_FR_ns, &dts)
542+
dt64_to_dtstruct(stamps[i] + delta, &dts)
548543
curr_reso = _reso_stamp(&dts)
549544
if curr_reso < reso:
550545
reso = curr_reso
@@ -562,17 +557,15 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
562557
for i in range(n):
563558
if stamps[i] == NPY_NAT:
564559
continue
565-
pandas_datetime_to_datetimestruct(stamps[i] + deltas[0],
566-
PANDAS_FR_ns, &dts)
560+
dt64_to_dtstruct(stamps[i] + deltas[0], &dts)
567561
curr_reso = _reso_stamp(&dts)
568562
if curr_reso < reso:
569563
reso = curr_reso
570564
else:
571565
for i in range(n):
572566
if stamps[i] == NPY_NAT:
573567
continue
574-
pandas_datetime_to_datetimestruct(stamps[i] + deltas[pos[i]],
575-
PANDAS_FR_ns, &dts)
568+
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
576569
curr_reso = _reso_stamp(&dts)
577570
if curr_reso < reso:
578571
reso = curr_reso
@@ -595,7 +588,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
595588
if stamps[i] == NPY_NAT:
596589
result[i] = NPY_NAT
597590
continue
598-
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
591+
dt64_to_dtstruct(stamps[i], &dts)
599592
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
600593
dts.hour, dts.min, dts.sec,
601594
dts.us, dts.ps, freq)
@@ -605,13 +598,11 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
605598
if stamps[i] == NPY_NAT:
606599
result[i] = NPY_NAT
607600
continue
608-
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns,
609-
&dts)
601+
dt64_to_dtstruct(stamps[i], &dts)
610602
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
611603
dts.min, dts.sec, dts.us, tz)
612604
delta = int(get_utcoffset(tz, dt).total_seconds()) * 1000000000
613-
pandas_datetime_to_datetimestruct(stamps[i] + delta,
614-
PANDAS_FR_ns, &dts)
605+
dt64_to_dtstruct(stamps[i] + delta, &dts)
615606
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
616607
dts.hour, dts.min, dts.sec,
617608
dts.us, dts.ps, freq)
@@ -630,8 +621,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
630621
if stamps[i] == NPY_NAT:
631622
result[i] = NPY_NAT
632623
continue
633-
pandas_datetime_to_datetimestruct(stamps[i] + deltas[0],
634-
PANDAS_FR_ns, &dts)
624+
dt64_to_dtstruct(stamps[i] + deltas[0], &dts)
635625
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
636626
dts.hour, dts.min, dts.sec,
637627
dts.us, dts.ps, freq)
@@ -640,8 +630,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
640630
if stamps[i] == NPY_NAT:
641631
result[i] = NPY_NAT
642632
continue
643-
pandas_datetime_to_datetimestruct(stamps[i] + deltas[pos[i]],
644-
PANDAS_FR_ns, &dts)
633+
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
645634
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
646635
dts.hour, dts.min, dts.sec,
647636
dts.us, dts.ps, freq)

pandas/_libs/tslib.pyx

+18-24
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ cimport util
3333
from cpython.datetime cimport PyDelta_Check, PyTZInfo_Check
3434
# this is our datetime.pxd
3535
from datetime cimport (
36-
pandas_datetimestruct,
3736
pandas_datetime_to_datetimestruct,
3837
pandas_datetimestruct_to_datetime,
3938
days_per_month_table,
@@ -50,14 +49,15 @@ from datetime cimport (
5049
PANDAS_FR_ns,
5150
PyDateTime_Check, PyDate_Check,
5251
PyDateTime_IMPORT,
53-
timedelta, datetime
54-
)
52+
timedelta, datetime)
5553

5654
# stdlib datetime imports
5755
from datetime import timedelta, datetime
5856
from datetime import time as datetime_time
5957

60-
from tslibs.np_datetime cimport check_dts_bounds
58+
from tslibs.np_datetime cimport (check_dts_bounds,
59+
pandas_datetimestruct,
60+
dt64_to_dtstruct, dtstruct_to_dt64)
6161
from tslibs.np_datetime import OutOfBoundsDatetime
6262

6363
from khash cimport (
@@ -149,17 +149,15 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
149149
if value == NPY_NAT:
150150
result[i] = NaT
151151
else:
152-
pandas_datetime_to_datetimestruct(
153-
value, PANDAS_FR_ns, &dts)
152+
dt64_to_dtstruct(value, &dts)
154153
result[i] = func_create(value, dts, tz, freq)
155154
elif is_tzlocal(tz) or is_fixed_offset(tz):
156155
for i in range(n):
157156
value = arr[i]
158157
if value == NPY_NAT:
159158
result[i] = NaT
160159
else:
161-
pandas_datetime_to_datetimestruct(
162-
value, PANDAS_FR_ns, &dts)
160+
dt64_to_dtstruct(value, &dts)
163161
dt = create_datetime_from_ts(value, dts, tz, freq)
164162
dt = dt + tz.utcoffset(dt)
165163
if box:
@@ -185,8 +183,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
185183
# represented in single object.
186184
new_tz = tz
187185

188-
pandas_datetime_to_datetimestruct(
189-
value + deltas[pos], PANDAS_FR_ns, &dts)
186+
dt64_to_dtstruct(value + deltas[pos], &dts)
190187
result[i] = func_create(value, dts, new_tz, freq)
191188
else:
192189
for i in range(n):
@@ -195,7 +192,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
195192
if value == NPY_NAT:
196193
result[i] = NaT
197194
else:
198-
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
195+
dt64_to_dtstruct(value, &dts)
199196
result[i] = func_create(value, dts, None, freq)
200197

201198
return result
@@ -698,7 +695,7 @@ class Timestamp(_Timestamp):
698695
value += value - value_tz
699696

700697
# setup components
701-
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
698+
dt64_to_dtstruct(value, &dts)
702699
dts.ps = self.nanosecond * 1000
703700

704701
# replace
@@ -1811,7 +1808,7 @@ def _test_parse_iso8601(object ts):
18111808
obj = _TSObject()
18121809

18131810
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
1814-
obj.value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &obj.dts)
1811+
obj.value = dtstruct_to_dt64(&obj.dts)
18151812
check_dts_bounds(&obj.dts)
18161813
if out_local == 1:
18171814
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
@@ -1984,7 +1981,7 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
19841981
result[i] = na_rep
19851982
elif basic_format:
19861983

1987-
pandas_datetime_to_datetimestruct(val, PANDAS_FR_ns, &dts)
1984+
dt64_to_dtstruct(val, &dts)
19881985
res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year,
19891986
dts.month,
19901987
dts.day,
@@ -3792,7 +3789,7 @@ cdef inline int64_t _normalized_stamp(pandas_datetimestruct *dts) nogil:
37923789
dts.sec = 0
37933790
dts.us = 0
37943791
dts.ps = 0
3795-
return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, dts)
3792+
return dtstruct_to_dt64(dts)
37963793

37973794

37983795
def dates_normalized(ndarray[int64_t] stamps, tz=None):
@@ -3910,13 +3907,12 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
39103907
out[i] = NPY_NAT
39113908
continue
39123909

3913-
pandas_datetime_to_datetimestruct(dtindex[i],
3914-
PANDAS_FR_ns, &dts)
3910+
dt64_to_dtstruct(dtindex[i], &dts)
39153911
dts.year = _year_add_months(dts, months)
39163912
dts.month = _month_add_months(dts, months)
39173913

39183914
dts.day = min(dts.day, days_in_month(dts))
3919-
out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
3915+
out[i] = dtstruct_to_dt64(&dts)
39203916
elif day == 'start':
39213917
roll_check = False
39223918
if months <= 0:
@@ -3928,8 +3924,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
39283924
out[i] = NPY_NAT
39293925
continue
39303926

3931-
pandas_datetime_to_datetimestruct(dtindex[i],
3932-
PANDAS_FR_ns, &dts)
3927+
dt64_to_dtstruct(dtindex[i], &dts)
39333928
months_to_roll = months
39343929

39353930
# offset semantics - if on the anchor point and going backwards
@@ -3941,7 +3936,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
39413936
dts.month = _month_add_months(dts, months_to_roll)
39423937
dts.day = 1
39433938

3944-
out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
3939+
out[i] = dtstruct_to_dt64(&dts)
39453940
elif day == 'end':
39463941
roll_check = False
39473942
if months > 0:
@@ -3953,8 +3948,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
39533948
out[i] = NPY_NAT
39543949
continue
39553950

3956-
pandas_datetime_to_datetimestruct(dtindex[i],
3957-
PANDAS_FR_ns, &dts)
3951+
dt64_to_dtstruct(dtindex[i], &dts)
39583952
months_to_roll = months
39593953

39603954
# similar semantics - when adding shift forward by one
@@ -3966,7 +3960,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
39663960
dts.month = _month_add_months(dts, months_to_roll)
39673961

39683962
dts.day = days_in_month(dts)
3969-
out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
3963+
out[i] = dtstruct_to_dt64(&dts)
39703964
else:
39713965
raise ValueError("day must be None, 'start' or 'end'")
39723966

0 commit comments

Comments
 (0)