@@ -4,7 +4,10 @@ cimport cython
4
4
5
5
from enum import Enum
6
6
7
- from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
7
+ from pandas._libs.tslibs.np_datetime cimport (
8
+ NPY_DATETIMEUNIT,
9
+ get_conversion_factor,
10
+ )
8
11
9
12
10
13
cdef class PeriodDtypeBase:
@@ -386,83 +389,11 @@ cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns)
386
389
"""
387
390
How many of the given time units fit into a single day?
388
391
"""
389
- cdef:
390
- int64_t day_units
391
-
392
- if reso == NPY_DATETIMEUNIT.NPY_FR_ps:
393
- # pico is the smallest unit for which we don't overflow, so
394
- # we exclude femto and atto
395
- day_units = 24 * 3600 * 1 _000_000_000_000
396
- elif reso == NPY_DATETIMEUNIT.NPY_FR_ns:
397
- day_units = 24 * 3600 * 1 _000_000_000
398
- elif reso == NPY_DATETIMEUNIT.NPY_FR_us:
399
- day_units = 24 * 3600 * 1 _000_000
400
- elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
401
- day_units = 24 * 3600 * 1 _000
402
- elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
403
- day_units = 24 * 3600
404
- elif reso == NPY_DATETIMEUNIT.NPY_FR_m:
405
- day_units = 24 * 60
406
- elif reso == NPY_DATETIMEUNIT.NPY_FR_h:
407
- day_units = 24
408
- elif reso == NPY_DATETIMEUNIT.NPY_FR_D:
409
- day_units = 1
410
- else :
411
- raise NotImplementedError (reso)
412
- return day_units
392
+ return get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_D, reso)
413
393
414
394
415
395
cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except ? - 1 :
416
- if reso == NPY_DATETIMEUNIT.NPY_FR_ns:
417
- return 1 _000_000_000
418
- elif reso == NPY_DATETIMEUNIT.NPY_FR_us:
419
- return 1 _000_000
420
- elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
421
- return 1 _000
422
- elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
423
- return 1
424
- else :
425
- raise NotImplementedError (reso)
426
-
427
-
428
- @ cython.overflowcheck (True )
429
- cdef int64_t get_conversion_factor(NPY_DATETIMEUNIT from_unit, NPY_DATETIMEUNIT to_unit) except ? - 1 :
430
- """
431
- Find the factor by which we need to multiply to convert from from_unit to to_unit.
432
- """
433
- if (
434
- from_unit == NPY_DATETIMEUNIT.NPY_FR_GENERIC
435
- or to_unit == NPY_DATETIMEUNIT.NPY_FR_GENERIC
436
- ):
437
- raise ValueError (" unit-less resolutions are not supported" )
438
- if from_unit > to_unit:
439
- raise ValueError
440
-
441
- if from_unit == to_unit:
442
- return 1
443
-
444
- if from_unit == NPY_DATETIMEUNIT.NPY_FR_W:
445
- return 7 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_D, to_unit)
446
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_D:
447
- return 24 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_h, to_unit)
448
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_h:
449
- return 60 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_m, to_unit)
450
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_m:
451
- return 60 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_s, to_unit)
452
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_s:
453
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ms, to_unit)
454
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ms:
455
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_us, to_unit)
456
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_us:
457
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ns, to_unit)
458
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ns:
459
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ps, to_unit)
460
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ps:
461
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_fs, to_unit)
462
- elif from_unit == NPY_DATETIMEUNIT.NPY_FR_fs:
463
- return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_as, to_unit)
464
- else :
465
- raise ValueError (from_unit, to_unit)
396
+ return get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_s, reso)
466
397
467
398
468
399
cdef dict _reso_str_map = {
0 commit comments