|
1 | 1 | # period frequency constants corresponding to scikits timeseries
|
2 | 2 | # originals
|
| 3 | +cimport cython |
| 4 | + |
3 | 5 | from enum import Enum
|
4 | 6 |
|
5 | 7 | from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
|
@@ -361,6 +363,46 @@ cdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1:
|
361 | 363 | raise NotImplementedError(reso)
|
362 | 364 |
|
363 | 365 |
|
| 366 | +@cython.overflowcheck(True) |
| 367 | +cdef int64_t get_conversion_factor(NPY_DATETIMEUNIT from_unit, NPY_DATETIMEUNIT to_unit): |
| 368 | + """ |
| 369 | + Find the factor by which we need to multiply to convert from from_unit to to_unit. |
| 370 | + """ |
| 371 | + if ( |
| 372 | + from_unit == NPY_DATETIMEUNIT.NPY_FR_GENERIC |
| 373 | + or to_unit == NPY_DATETIMEUNIT.NPY_FR_GENERIC |
| 374 | + ): |
| 375 | + raise ValueError("unit-less resolutions are not supported") |
| 376 | + if from_unit > to_unit: |
| 377 | + raise ValueError |
| 378 | + |
| 379 | + if from_unit == to_unit: |
| 380 | + return 1 |
| 381 | + |
| 382 | + if from_unit == NPY_DATETIMEUNIT.NPY_FR_W: |
| 383 | + return 7 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_D, to_unit) |
| 384 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_D: |
| 385 | + return 24 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_h, to_unit) |
| 386 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_h: |
| 387 | + return 60 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_m, to_unit) |
| 388 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_m: |
| 389 | + return 60 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_s, to_unit) |
| 390 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_s: |
| 391 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ms, to_unit) |
| 392 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ms: |
| 393 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_us, to_unit) |
| 394 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_us: |
| 395 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ns, to_unit) |
| 396 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ns: |
| 397 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_ps, to_unit) |
| 398 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_ps: |
| 399 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_fs, to_unit) |
| 400 | + elif from_unit == NPY_DATETIMEUNIT.NPY_FR_fs: |
| 401 | + return 1000 * get_conversion_factor(NPY_DATETIMEUNIT.NPY_FR_as, to_unit) |
| 402 | + else: |
| 403 | + raise ValueError(from_unit, to_unit) |
| 404 | + |
| 405 | + |
364 | 406 | cdef dict _reso_str_map = {
|
365 | 407 | Resolution.RESO_NS.value: "nanosecond",
|
366 | 408 | Resolution.RESO_US.value: "microsecond",
|
|
0 commit comments