Skip to content

Commit c3cfe90

Browse files
jbrockmendeljreback
authored andcommitted
Tslibs resolution (#18034)
1 parent 4292a27 commit c3cfe90

File tree

11 files changed

+717
-682
lines changed

11 files changed

+717
-682
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Other API Changes
4545
- :class:`Timestamp` will no longer silently ignore unused or invalid ``tz`` or ``tzinfo`` keyword arguments (:issue:`17690`)
4646
- :class:`Timestamp` will no longer silently ignore invalid ``freq`` arguments (:issue:`5168`)
4747
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the ``pandas.tseries.offsets`` module (:issue:`17830`)
48+
- `tseries.frequencies.get_freq_group()` and `tseries.frequencies.DAYS` are removed from the public API (:issue:`18034`)
4849

4950
.. _whatsnew_0220.deprecations:
5051

pandas/_libs/period.pyx

+10-102
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
# cython: profile=False
23
from datetime import datetime, date, timedelta
34
import operator
45

@@ -27,28 +28,23 @@ from util cimport is_period_object, is_string_object, INT32_MIN
2728

2829
from lib cimport is_null_datetimelike
2930
from pandas._libs import tslib
30-
from pandas._libs.tslib import Timestamp, iNaT, NaT
31+
from pandas._libs.tslib import Timestamp, iNaT
3132
from tslibs.timezones cimport (
3233
is_utc, is_tzlocal, get_utcoffset, get_dst_info, maybe_get_tz)
3334
from tslibs.timedeltas cimport delta_to_nanoseconds
3435

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)
3638
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
3841
from tslibs.nattype cimport _nat_scalar_rules
3942

4043
from pandas.tseries import offsets
4144
from pandas.tseries import frequencies
4245

4346
cdef int64_t NPY_NAT = util.get_nat()
4447

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-
5248
cdef extern from "period_helper.h":
5349
ctypedef struct date_info:
5450
int64_t absdate
@@ -487,98 +483,10 @@ def extract_freq(ndarray[object] values):
487483
raise ValueError('freq not specified and cannot be inferred')
488484

489485

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+
# -----------------------------------------------------------------------
580487
# period helpers
581488

489+
582490
cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
583491
int freq, object tz):
584492
cdef:
@@ -1191,7 +1099,7 @@ class Period(_Period):
11911099

11921100
if freq is None:
11931101
try:
1194-
freq = frequencies.Resolution.get_freq(reso)
1102+
freq = Resolution.get_freq(reso)
11951103
except KeyError:
11961104
raise ValueError(
11971105
"Invalid frequency or could not infer: %s" % reso)
@@ -1236,7 +1144,7 @@ def _quarter_to_myear(year, quarter, freq):
12361144
if quarter <= 0 or quarter > 4:
12371145
raise ValueError('Quarter must be 1 <= q <= 4')
12381146

1239-
mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
1147+
mnum = _MONTH_NUMBERS[_get_rule_month(freq)] + 1
12401148
month = (mnum + (quarter - 1) * 3) % 12 + 1
12411149
if month > mnum:
12421150
year -= 1

pandas/_libs/tslib.pyx

+1-54
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ from datetime cimport (
5050
# stdlib datetime imports
5151
from datetime import time as datetime_time
5252

53+
5354
from tslibs.np_datetime cimport (check_dts_bounds,
5455
reverse_ops,
5556
cmp_scalar,
@@ -61,12 +62,6 @@ from tslibs.np_datetime cimport (check_dts_bounds,
6162
get_timedelta64_value)
6263
from tslibs.np_datetime import OutOfBoundsDatetime
6364

64-
from khash cimport (
65-
khiter_t,
66-
kh_destroy_int64, kh_put_int64,
67-
kh_init_int64, kh_int64_t,
68-
kh_resize_int64, kh_get_int64)
69-
7065
from .tslibs.parsing import parse_datetime_string
7166

7267
cimport cython
@@ -877,33 +872,6 @@ Timestamp.min = Timestamp(_NS_LOWER_BOUND)
877872
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
878873

879874

880-
# ----------------------------------------------------------------------
881-
# Frequency inference
882-
883-
def unique_deltas(ndarray[int64_t] arr):
884-
cdef:
885-
Py_ssize_t i, n = len(arr)
886-
int64_t val
887-
khiter_t k
888-
kh_int64_t *table
889-
int ret = 0
890-
list uniques = []
891-
892-
table = kh_init_int64()
893-
kh_resize_int64(table, 10)
894-
for i in range(n - 1):
895-
val = arr[i + 1] - arr[i]
896-
k = kh_get_int64(table, val)
897-
if k == table.n_buckets:
898-
kh_put_int64(table, val, &ret)
899-
uniques.append(val)
900-
kh_destroy_int64(table)
901-
902-
result = np.array(uniques, dtype=np.int64)
903-
result.sort()
904-
return result
905-
906-
907875
cdef str _NDIM_STRING = "ndim"
908876

909877
# This is PITA. Because we inherit from datetime, which has very specific
@@ -1388,27 +1356,6 @@ _MONTH_NUMBERS = {k: i for i, k in enumerate(_MONTHS)}
13881356
_MONTH_ALIASES = {(k + 1): v for k, v in enumerate(_MONTHS)}
13891357

13901358

1391-
cpdef object _get_rule_month(object source, object default='DEC'):
1392-
"""
1393-
Return starting month of given freq, default is December.
1394-
1395-
Example
1396-
-------
1397-
>>> _get_rule_month('D')
1398-
'DEC'
1399-
1400-
>>> _get_rule_month('A-JAN')
1401-
'JAN'
1402-
"""
1403-
if hasattr(source, 'freqstr'):
1404-
source = source.freqstr
1405-
source = source.upper()
1406-
if '-' not in source:
1407-
return default
1408-
else:
1409-
return source.split('-')[1]
1410-
1411-
14121359
cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
14131360
"""
14141361
convert the ndarray according to the unit

0 commit comments

Comments
 (0)