Skip to content

Commit 5414a2d

Browse files
authored
REF: implement periods_per_day (pandas-dev#46462)
1 parent d5f2e5c commit 5414a2d

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

pandas/_libs/tslibs/dtypes.pxd

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from numpy cimport int64_t
2+
13
from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
24

35

46
cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
57
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
8+
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*)
69

710
cdef dict attrname_to_abbrevs
811

pandas/_libs/tslibs/dtypes.pyx

+30
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,36 @@ cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
307307
return NPY_DATETIMEUNIT.NPY_FR_D
308308

309309

310+
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns):
311+
"""
312+
How many of the given time units fit into a single day?
313+
"""
314+
cdef:
315+
int64_t day_units
316+
317+
if reso == NPY_DATETIMEUNIT.NPY_FR_ps:
318+
# pico is the smallest unit for which we don't overflow, so
319+
# we exclude fempto and atto
320+
day_units = 24 * 3600 * 1_000_000_000_000
321+
elif reso == NPY_DATETIMEUNIT.NPY_FR_ns:
322+
day_units = 24 * 3600 * 1_000_000_000
323+
elif reso == NPY_DATETIMEUNIT.NPY_FR_us:
324+
day_units = 24 * 3600 * 1_000_000
325+
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
326+
day_units = 24 * 3600 * 1_000
327+
elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
328+
day_units = 24 * 3600
329+
elif reso == NPY_DATETIMEUNIT.NPY_FR_m:
330+
day_units = 24 * 60
331+
elif reso == NPY_DATETIMEUNIT.NPY_FR_h:
332+
day_units = 24
333+
elif reso == NPY_DATETIMEUNIT.NPY_FR_D:
334+
day_units = 1
335+
else:
336+
raise NotImplementedError(reso)
337+
return day_units
338+
339+
310340
cdef dict _reso_str_map = {
311341
Resolution.RESO_NS.value: "nanosecond",
312342
Resolution.RESO_US.value: "microsecond",

pandas/_libs/tslibs/vectorized.pyx

+10-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ cnp.import_array()
2121
from .conversion cimport normalize_i8_stamp
2222

2323
from .dtypes import Resolution
24+
2425
from .ccalendar cimport DAY_NANOS
26+
from .dtypes cimport c_Resolution
2527
from .nattype cimport (
2628
NPY_NAT,
2729
c_NaT as NaT,
@@ -168,27 +170,19 @@ def ints_to_pydatetime(
168170

169171
# -------------------------------------------------------------------------
170172

171-
cdef:
172-
int RESO_US = Resolution.RESO_US.value
173-
int RESO_MS = Resolution.RESO_MS.value
174-
int RESO_SEC = Resolution.RESO_SEC.value
175-
int RESO_MIN = Resolution.RESO_MIN.value
176-
int RESO_HR = Resolution.RESO_HR.value
177-
int RESO_DAY = Resolution.RESO_DAY.value
178-
179173

180-
cdef inline int _reso_stamp(npy_datetimestruct *dts):
174+
cdef inline c_Resolution _reso_stamp(npy_datetimestruct *dts):
181175
if dts.us != 0:
182176
if dts.us % 1000 == 0:
183-
return RESO_MS
184-
return RESO_US
177+
return c_Resolution.RESO_MS
178+
return c_Resolution.RESO_US
185179
elif dts.sec != 0:
186-
return RESO_SEC
180+
return c_Resolution.RESO_SEC
187181
elif dts.min != 0:
188-
return RESO_MIN
182+
return c_Resolution.RESO_MIN
189183
elif dts.hour != 0:
190-
return RESO_HR
191-
return RESO_DAY
184+
return c_Resolution.RESO_HR
185+
return c_Resolution.RESO_DAY
192186

193187

194188
@cython.wraparound(False)
@@ -205,7 +199,7 @@ def get_resolution(const int64_t[:] stamps, tzinfo tz=None) -> Resolution:
205199
str typ
206200

207201
npy_datetimestruct dts
208-
int reso = RESO_DAY, curr_reso
202+
c_Resolution reso = c_Resolution.RESO_DAY, curr_reso
209203

210204
if is_utc(tz) or tz is None:
211205
use_utc = True

0 commit comments

Comments
 (0)