|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +# cython: profile=False |
2 | 3 | from datetime import datetime, date, timedelta
|
3 | 4 | import operator
|
4 | 5 |
|
@@ -27,28 +28,23 @@ from util cimport is_period_object, is_string_object, INT32_MIN
|
27 | 28 |
|
28 | 29 | from lib cimport is_null_datetimelike
|
29 | 30 | from pandas._libs import tslib
|
30 |
| -from pandas._libs.tslib import Timestamp, iNaT, NaT |
| 31 | +from pandas._libs.tslib import Timestamp, iNaT |
31 | 32 | from tslibs.timezones cimport (
|
32 | 33 | is_utc, is_tzlocal, get_utcoffset, get_dst_info, maybe_get_tz)
|
33 | 34 | from tslibs.timedeltas cimport delta_to_nanoseconds
|
34 | 35 |
|
35 |
| -from tslibs.parsing import parse_time_string, NAT_SENTINEL |
| 36 | +from tslibs.parsing import (parse_time_string, NAT_SENTINEL, |
| 37 | + _get_rule_month, _MONTH_NUMBERS) |
36 | 38 | from tslibs.frequencies cimport get_freq_code
|
37 |
| -from tslibs.nattype import nat_strings |
| 39 | +from tslibs.resolution import resolution, Resolution |
| 40 | +from tslibs.nattype import nat_strings, NaT |
38 | 41 | from tslibs.nattype cimport _nat_scalar_rules
|
39 | 42 |
|
40 | 43 | from pandas.tseries import offsets
|
41 | 44 | from pandas.tseries import frequencies
|
42 | 45 |
|
43 | 46 | cdef int64_t NPY_NAT = util.get_nat()
|
44 | 47 |
|
45 |
| -cdef int RESO_US = frequencies.RESO_US |
46 |
| -cdef int RESO_MS = frequencies.RESO_MS |
47 |
| -cdef int RESO_SEC = frequencies.RESO_SEC |
48 |
| -cdef int RESO_MIN = frequencies.RESO_MIN |
49 |
| -cdef int RESO_HR = frequencies.RESO_HR |
50 |
| -cdef int RESO_DAY = frequencies.RESO_DAY |
51 |
| - |
52 | 48 | cdef extern from "period_helper.h":
|
53 | 49 | ctypedef struct date_info:
|
54 | 50 | int64_t absdate
|
@@ -487,98 +483,10 @@ def extract_freq(ndarray[object] values):
|
487 | 483 | raise ValueError('freq not specified and cannot be inferred')
|
488 | 484 |
|
489 | 485 |
|
490 |
| -cpdef resolution(ndarray[int64_t] stamps, tz=None): |
491 |
| - cdef: |
492 |
| - Py_ssize_t i, n = len(stamps) |
493 |
| - pandas_datetimestruct dts |
494 |
| - int reso = RESO_DAY, curr_reso |
495 |
| - |
496 |
| - if tz is not None: |
497 |
| - tz = maybe_get_tz(tz) |
498 |
| - return _reso_local(stamps, tz) |
499 |
| - else: |
500 |
| - for i in range(n): |
501 |
| - if stamps[i] == NPY_NAT: |
502 |
| - continue |
503 |
| - dt64_to_dtstruct(stamps[i], &dts) |
504 |
| - curr_reso = _reso_stamp(&dts) |
505 |
| - if curr_reso < reso: |
506 |
| - reso = curr_reso |
507 |
| - return reso |
508 |
| - |
509 |
| - |
510 |
| -cdef inline int _reso_stamp(pandas_datetimestruct *dts): |
511 |
| - if dts.us != 0: |
512 |
| - if dts.us % 1000 == 0: |
513 |
| - return RESO_MS |
514 |
| - return RESO_US |
515 |
| - elif dts.sec != 0: |
516 |
| - return RESO_SEC |
517 |
| - elif dts.min != 0: |
518 |
| - return RESO_MIN |
519 |
| - elif dts.hour != 0: |
520 |
| - return RESO_HR |
521 |
| - return RESO_DAY |
522 |
| - |
523 |
| -cdef _reso_local(ndarray[int64_t] stamps, object tz): |
524 |
| - cdef: |
525 |
| - Py_ssize_t n = len(stamps) |
526 |
| - int reso = RESO_DAY, curr_reso |
527 |
| - ndarray[int64_t] trans, deltas, pos |
528 |
| - pandas_datetimestruct dts |
529 |
| - |
530 |
| - if is_utc(tz): |
531 |
| - for i in range(n): |
532 |
| - if stamps[i] == NPY_NAT: |
533 |
| - continue |
534 |
| - dt64_to_dtstruct(stamps[i], &dts) |
535 |
| - curr_reso = _reso_stamp(&dts) |
536 |
| - if curr_reso < reso: |
537 |
| - reso = curr_reso |
538 |
| - elif is_tzlocal(tz): |
539 |
| - for i in range(n): |
540 |
| - if stamps[i] == NPY_NAT: |
541 |
| - continue |
542 |
| - dt64_to_dtstruct(stamps[i], &dts) |
543 |
| - dt = datetime(dts.year, dts.month, dts.day, dts.hour, |
544 |
| - dts.min, dts.sec, dts.us, tz) |
545 |
| - delta = int(get_utcoffset(tz, dt).total_seconds()) * 1000000000 |
546 |
| - dt64_to_dtstruct(stamps[i] + delta, &dts) |
547 |
| - curr_reso = _reso_stamp(&dts) |
548 |
| - if curr_reso < reso: |
549 |
| - reso = curr_reso |
550 |
| - else: |
551 |
| - # Adjust datetime64 timestamp, recompute datetimestruct |
552 |
| - trans, deltas, typ = get_dst_info(tz) |
553 |
| - |
554 |
| - _pos = trans.searchsorted(stamps, side='right') - 1 |
555 |
| - if _pos.dtype != np.int64: |
556 |
| - _pos = _pos.astype(np.int64) |
557 |
| - pos = _pos |
558 |
| - |
559 |
| - # statictzinfo |
560 |
| - if typ not in ['pytz', 'dateutil']: |
561 |
| - for i in range(n): |
562 |
| - if stamps[i] == NPY_NAT: |
563 |
| - continue |
564 |
| - dt64_to_dtstruct(stamps[i] + deltas[0], &dts) |
565 |
| - curr_reso = _reso_stamp(&dts) |
566 |
| - if curr_reso < reso: |
567 |
| - reso = curr_reso |
568 |
| - else: |
569 |
| - for i in range(n): |
570 |
| - if stamps[i] == NPY_NAT: |
571 |
| - continue |
572 |
| - dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts) |
573 |
| - curr_reso = _reso_stamp(&dts) |
574 |
| - if curr_reso < reso: |
575 |
| - reso = curr_reso |
576 |
| - |
577 |
| - return reso |
578 |
| - |
579 |
| - |
| 486 | +# ----------------------------------------------------------------------- |
580 | 487 | # period helpers
|
581 | 488 |
|
| 489 | + |
582 | 490 | cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
|
583 | 491 | int freq, object tz):
|
584 | 492 | cdef:
|
@@ -1191,7 +1099,7 @@ class Period(_Period):
|
1191 | 1099 |
|
1192 | 1100 | if freq is None:
|
1193 | 1101 | try:
|
1194 |
| - freq = frequencies.Resolution.get_freq(reso) |
| 1102 | + freq = Resolution.get_freq(reso) |
1195 | 1103 | except KeyError:
|
1196 | 1104 | raise ValueError(
|
1197 | 1105 | "Invalid frequency or could not infer: %s" % reso)
|
@@ -1236,7 +1144,7 @@ def _quarter_to_myear(year, quarter, freq):
|
1236 | 1144 | if quarter <= 0 or quarter > 4:
|
1237 | 1145 | raise ValueError('Quarter must be 1 <= q <= 4')
|
1238 | 1146 |
|
1239 |
| - mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1 |
| 1147 | + mnum = _MONTH_NUMBERS[_get_rule_month(freq)] + 1 |
1240 | 1148 | month = (mnum + (quarter - 1) * 3) % 12 + 1
|
1241 | 1149 | if month > mnum:
|
1242 | 1150 | year -= 1
|
|
0 commit comments