@@ -4,18 +4,14 @@ from cpython.datetime cimport (
4
4
PyDate_Check,
5
5
PyDateTime_Check,
6
6
PyDateTime_IMPORT,
7
- date,
8
7
datetime,
9
- time,
10
- timedelta,
11
- tzinfo,
12
8
)
13
9
# import datetime C API
14
10
PyDateTime_IMPORT
15
11
16
12
17
13
cimport numpy as cnp
18
- from numpy cimport float64_t, int64_t, ndarray, uint8_t, intp_t
14
+ from numpy cimport float64_t, int64_t, ndarray
19
15
import numpy as np
20
16
cnp.import_array()
21
17
@@ -42,11 +38,6 @@ from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
42
38
43
39
from pandas._libs.tslibs.parsing import parse_datetime_string
44
40
45
- from pandas._libs.tslibs.timezones cimport (
46
- get_dst_info,
47
- is_utc,
48
- is_tzlocal,
49
- )
50
41
from pandas._libs.tslibs.conversion cimport (
51
42
_TSObject,
52
43
cast_from_unit,
@@ -60,174 +51,17 @@ from pandas._libs.tslibs.nattype cimport (
60
51
c_nat_strings as nat_strings,
61
52
)
62
53
63
- from pandas._libs.tslibs.offsets cimport to_offset
64
-
65
- from pandas._libs.tslibs.timestamps cimport create_timestamp_from_ts, _Timestamp
54
+ from pandas._libs.tslibs.timestamps cimport _Timestamp
66
55
from pandas._libs.tslibs.timestamps import Timestamp
67
56
68
57
from pandas._libs.tslibs.tzconversion cimport (
69
- tz_convert_utc_to_tzlocal,
70
58
tz_localize_to_utc_single,
71
59
)
72
60
73
61
# Note: this is the only non-tslibs intra-pandas dependency here
74
62
from pandas._libs.missing cimport checknull_with_nat_and_na
75
63
76
64
77
- cdef inline object create_datetime_from_ts(
78
- int64_t value,
79
- npy_datetimestruct dts,
80
- tzinfo tz,
81
- object freq,
82
- bint fold,
83
- ):
84
- """
85
- Convenience routine to construct a datetime.datetime from its parts.
86
- """
87
- return datetime(
88
- dts.year, dts.month, dts.day, dts.hour, dts.min, dts.sec, dts.us, tz, fold = fold
89
- )
90
-
91
-
92
- cdef inline object create_date_from_ts(
93
- int64_t value,
94
- npy_datetimestruct dts,
95
- tzinfo tz,
96
- object freq,
97
- bint fold
98
- ):
99
- """
100
- Convenience routine to construct a datetime.date from its parts.
101
- """
102
- # GH 25057 add fold argument to match other func_create signatures
103
- return date(dts.year, dts.month, dts.day)
104
-
105
-
106
- cdef inline object create_time_from_ts(
107
- int64_t value,
108
- npy_datetimestruct dts,
109
- tzinfo tz,
110
- object freq,
111
- bint fold
112
- ):
113
- """
114
- Convenience routine to construct a datetime.time from its parts.
115
- """
116
- return time(dts.hour, dts.min, dts.sec, dts.us, tz, fold = fold)
117
-
118
-
119
- @ cython.wraparound (False )
120
- @ cython.boundscheck (False )
121
- def ints_to_pydatetime (
122
- const int64_t[:] arr ,
123
- tzinfo tz = None ,
124
- object freq = None ,
125
- bint fold = False ,
126
- str box = " datetime"
127
- ):
128
- """
129
- Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp.
130
-
131
- Parameters
132
- ----------
133
- arr : array of i8
134
- tz : str, optional
135
- convert to this timezone
136
- freq : str/Offset, optional
137
- freq to convert
138
- fold : bint, default is 0
139
- Due to daylight saving time, one wall clock time can occur twice
140
- when shifting from summer to winter time; fold describes whether the
141
- datetime-like corresponds to the first (0) or the second time (1)
142
- the wall clock hits the ambiguous time
143
-
144
- .. versionadded:: 1.1.0
145
- box : {'datetime', 'timestamp', 'date', 'time'}, default 'datetime'
146
- * If datetime, convert to datetime.datetime
147
- * If date, convert to datetime.date
148
- * If time, convert to datetime.time
149
- * If Timestamp, convert to pandas.Timestamp
150
-
151
- Returns
152
- -------
153
- ndarray of dtype specified by box
154
- """
155
- cdef:
156
- Py_ssize_t i, n = len (arr)
157
- ndarray[int64_t] trans
158
- int64_t[:] deltas
159
- intp_t[:] pos
160
- npy_datetimestruct dts
161
- object dt, new_tz
162
- str typ
163
- int64_t value, local_value, delta = NPY_NAT # dummy for delta
164
- ndarray[object ] result = np.empty(n, dtype = object )
165
- object (* func_create)(int64_t, npy_datetimestruct, tzinfo, object , bint)
166
- bint use_utc = False , use_tzlocal = False , use_fixed = False
167
- bint use_pytz = False
168
-
169
- if box == " date" :
170
- assert (tz is None ), " tz should be None when converting to date"
171
-
172
- func_create = create_date_from_ts
173
- elif box == " timestamp" :
174
- func_create = create_timestamp_from_ts
175
-
176
- if isinstance (freq, str ):
177
- freq = to_offset(freq)
178
- elif box == " time" :
179
- func_create = create_time_from_ts
180
- elif box == " datetime" :
181
- func_create = create_datetime_from_ts
182
- else :
183
- raise ValueError (
184
- " box must be one of 'datetime', 'date', 'time' or 'timestamp'"
185
- )
186
-
187
- if is_utc(tz) or tz is None :
188
- use_utc = True
189
- elif is_tzlocal(tz):
190
- use_tzlocal = True
191
- else :
192
- trans, deltas, typ = get_dst_info(tz)
193
- if typ not in [" pytz" , " dateutil" ]:
194
- # static/fixed; in this case we know that len(delta) == 1
195
- use_fixed = True
196
- delta = deltas[0 ]
197
- else :
198
- pos = trans.searchsorted(arr, side = " right" ) - 1
199
- use_pytz = typ == " pytz"
200
-
201
- for i in range (n):
202
- new_tz = tz
203
- value = arr[i]
204
-
205
- if value == NPY_NAT:
206
- result[i] = < object > NaT
207
- else :
208
- if use_utc:
209
- local_value = value
210
- elif use_tzlocal:
211
- local_value = tz_convert_utc_to_tzlocal(value, tz)
212
- elif use_fixed:
213
- local_value = value + delta
214
- elif not use_pytz:
215
- # i.e. dateutil
216
- # no zone-name change for dateutil tzs - dst etc
217
- # represented in single object.
218
- local_value = value + deltas[pos[i]]
219
- else :
220
- # pytz
221
- # find right representation of dst etc in pytz timezone
222
- new_tz = tz._tzinfos[tz._transition_info[pos[i]]]
223
- local_value = value + deltas[pos[i]]
224
-
225
- dt64_to_dtstruct(local_value, & dts)
226
- result[i] = func_create(value, dts, new_tz, freq, fold)
227
-
228
- return result
229
-
230
-
231
65
def _test_parse_iso8601 (ts: str ):
232
66
"""
233
67
TESTING ONLY: Parse string into Timestamp using iso8601 parser. Used
0 commit comments