File tree 3 files changed +49
-11
lines changed
3 files changed +49
-11
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,10 @@ from cpython.datetime cimport (
31
31
import_datetime()
32
32
33
33
from pandas._libs.tslibs.base cimport ABCTimestamp
34
- from pandas._libs.tslibs.dtypes cimport periods_per_second
34
+ from pandas._libs.tslibs.dtypes cimport (
35
+ abbrev_to_npy_unit,
36
+ periods_per_second,
37
+ )
35
38
from pandas._libs.tslibs.np_datetime cimport (
36
39
NPY_DATETIMEUNIT,
37
40
NPY_FR_ns,
@@ -139,35 +142,36 @@ cpdef inline (int64_t, int) precision_from_unit(str unit):
139
142
cdef:
140
143
int64_t m
141
144
int p
145
+ NPY_DATETIMEUNIT reso = abbrev_to_npy_unit(unit)
142
146
143
- if unit == " Y " :
147
+ if reso == NPY_DATETIMEUNIT.NPY_FR_Y :
144
148
m = 1 _000_000_000 * 31556952
145
149
p = 9
146
- elif unit == " M " :
150
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_M :
147
151
m = 1 _000_000_000 * 2629746
148
152
p = 9
149
- elif unit == " W " :
153
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_W :
150
154
m = 1 _000_000_000 * 3600 * 24 * 7
151
155
p = 9
152
- elif unit == " D " or unit == " d " :
156
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_D :
153
157
m = 1 _000_000_000 * 3600 * 24
154
158
p = 9
155
- elif unit == " h " :
159
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_h :
156
160
m = 1 _000_000_000 * 3600
157
161
p = 9
158
- elif unit == " m " :
162
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_m :
159
163
m = 1 _000_000_000 * 60
160
164
p = 9
161
- elif unit == " s " :
165
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_s :
162
166
m = 1 _000_000_000
163
167
p = 9
164
- elif unit == " ms " :
168
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_ms :
165
169
m = 1 _000_000
166
170
p = 6
167
- elif unit == " us " :
171
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_us :
168
172
m = 1000
169
173
p = 3
170
- elif unit == " ns " or unit is None :
174
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_ns or reso == NPY_DATETIMEUNIT.NPY_FR_GENERIC :
171
175
m = 1
172
176
p = 0
173
177
else :
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
4
4
5
5
6
6
cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
7
+ cdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev)
7
8
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
8
9
cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso = * ) except ? - 1
9
10
cdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except ? - 1
Original file line number Diff line number Diff line change @@ -313,6 +313,39 @@ cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit):
313
313
raise NotImplementedError (unit)
314
314
315
315
316
+ cdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev):
317
+ if abbrev == " Y" :
318
+ return NPY_DATETIMEUNIT.NPY_FR_Y
319
+ elif abbrev == " M" :
320
+ return NPY_DATETIMEUNIT.NPY_FR_M
321
+ elif abbrev == " W" :
322
+ return NPY_DATETIMEUNIT.NPY_FR_W
323
+ elif abbrev == " D" or abbrev == " d" :
324
+ return NPY_DATETIMEUNIT.NPY_FR_D
325
+ elif abbrev == " h" :
326
+ return NPY_DATETIMEUNIT.NPY_FR_h
327
+ elif abbrev == " m" :
328
+ return NPY_DATETIMEUNIT.NPY_FR_m
329
+ elif abbrev == " s" :
330
+ return NPY_DATETIMEUNIT.NPY_FR_s
331
+ elif abbrev == " ms" :
332
+ return NPY_DATETIMEUNIT.NPY_FR_ms
333
+ elif abbrev == " us" :
334
+ return NPY_DATETIMEUNIT.NPY_FR_us
335
+ elif abbrev == " ns" :
336
+ return NPY_DATETIMEUNIT.NPY_FR_ns
337
+ elif abbrev == " ps" :
338
+ return NPY_DATETIMEUNIT.NPY_FR_ps
339
+ elif abbrev == " fs" :
340
+ return NPY_DATETIMEUNIT.NPY_FR_fs
341
+ elif abbrev == " as" :
342
+ return NPY_DATETIMEUNIT.NPY_FR_as
343
+ elif abbrev is None :
344
+ return NPY_DATETIMEUNIT.NPY_FR_GENERIC
345
+ else :
346
+ raise ValueError (f" Unrecognized unit {abbrev}" )
347
+
348
+
316
349
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
317
350
"""
318
351
Convert the freq to the corresponding NPY_DATETIMEUNIT to pass
You can’t perform that action at this time.
0 commit comments