1
- cimport numpy as cnp
2
- cnp.import_array()
3
1
4
- from pandas._libs.tslibs.util cimport is_integer_object
5
-
6
- from pandas._libs.tslibs.offsets cimport is_offset_object
7
- from pandas._libs.tslibs.offsets import (
8
- INVALID_FREQ_ERR_MSG,
9
- _dont_uppercase,
10
- _lite_rule_alias,
11
- base_and_stride,
12
- opattern,
13
- )
14
-
15
- from .dtypes import FreqGroup, _period_code_map, _reverse_period_code_map
2
+ from .dtypes import FreqGroup
16
3
17
4
# ---------------------------------------------------------------------
18
5
# Period codes
@@ -36,131 +23,22 @@ cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
36
23
37
24
# ----------------------------------------------------------------------
38
25
39
- def get_freq_group (freq ) -> int:
26
+ # TODO: this is now identical to the version in libperiod
27
+ def get_freq_group (freq: int ) -> int:
40
28
"""
41
29
Return frequency code group of given frequency str or offset.
42
30
43
31
Examples
44
32
--------
45
- >>> get_freq_group('W-MON' )
33
+ >>> get_freq_group(4001 )
46
34
4000
47
35
48
- >>> get_freq_group('W-FRI' )
36
+ >>> get_freq_group(4006 )
49
37
4000
50
38
"""
51
- if is_offset_object(freq ):
52
- freq = freq.rule_code
53
-
54
- if isinstance (freq, str ):
55
- freq = attrname_to_abbrevs.get(freq, freq)
56
- base, mult = get_freq_code(freq)
57
- freq = base
58
- elif isinstance (freq, int ):
59
- pass
60
- else :
61
- raise ValueError (' input must be str, offset or int' )
62
39
return (freq // 1000) * 1000
63
40
64
41
65
- cpdef get_freq_code(freqstr):
66
- """
67
- Return freq str or tuple to freq code and stride (mult)
68
-
69
- Parameters
70
- ----------
71
- freqstr : str or tuple
72
-
73
- Returns
74
- -------
75
- return : tuple of base frequency code and stride (mult)
76
-
77
- Raises
78
- ------
79
- TypeError : if passed a tuple witth incorrect types
80
-
81
- Examples
82
- --------
83
- >>> get_freq_code('3D')
84
- (6000, 3)
85
-
86
- >>> get_freq_code('D')
87
- (6000, 1)
88
-
89
- >>> get_freq_code(('D', 3))
90
- (6000, 3)
91
- """
92
- if is_offset_object(freqstr):
93
- freqstr = (freqstr.rule_code, freqstr.n)
94
-
95
- if isinstance (freqstr, tuple ):
96
- if is_integer_object(freqstr[0 ]) and is_integer_object(freqstr[1 ]):
97
- # e.g., freqstr = (2000, 1)
98
- return freqstr
99
- elif is_integer_object(freqstr[0 ]):
100
- # Note: passing freqstr[1] below will raise TypeError if that
101
- # is not a str
102
- code = _period_str_to_code(freqstr[1 ])
103
- stride = freqstr[0 ]
104
- return code, stride
105
- else :
106
- # e.g., freqstr = ('T', 5)
107
- code = _period_str_to_code(freqstr[0 ])
108
- stride = freqstr[1 ]
109
- return code, stride
110
-
111
- if is_integer_object(freqstr):
112
- return freqstr, 1
113
-
114
- base, stride = base_and_stride(freqstr)
115
- code = _period_str_to_code(base)
116
-
117
- return code, stride
118
-
119
-
120
- cpdef _period_str_to_code(str freqstr):
121
- freqstr = _lite_rule_alias.get(freqstr, freqstr)
122
-
123
- if freqstr not in _dont_uppercase:
124
- lower = freqstr.lower()
125
- freqstr = _lite_rule_alias.get(lower, freqstr)
126
-
127
- if freqstr not in _dont_uppercase:
128
- freqstr = freqstr.upper()
129
- try :
130
- return _period_code_map[freqstr]
131
- except KeyError :
132
- raise ValueError (INVALID_FREQ_ERR_MSG.format(freqstr))
133
-
134
-
135
- cpdef str get_freq_str(base, mult = 1 ):
136
- """
137
- Return the summary string associated with this offset code, possibly
138
- adjusted by a multiplier.
139
-
140
- Parameters
141
- ----------
142
- base : int (member of FreqGroup)
143
-
144
- Returns
145
- -------
146
- freq_str : str
147
-
148
- Examples
149
- --------
150
- >>> get_freq_str(1000)
151
- 'A-DEC'
152
-
153
- >>> get_freq_str(2000, 2)
154
- '2Q-DEC'
155
-
156
- >>> get_freq_str("foo")
157
- """
158
- code = _reverse_period_code_map.get(base)
159
- if mult == 1 :
160
- return code
161
- return str (mult) + code
162
-
163
-
164
42
cpdef int get_to_timestamp_base(int base ):
165
43
"""
166
44
Return frequency code group used for base of to_timestamp against
0 commit comments