forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvectorized.pyx
419 lines (356 loc) · 11.7 KB
/
vectorized.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import cython
from cpython.datetime cimport (
date,
datetime,
time,
tzinfo,
)
import numpy as np
cimport numpy as cnp
from numpy cimport (
int64_t,
intp_t,
ndarray,
)
cnp.import_array()
from .dtypes import Resolution
from .ccalendar cimport DAY_NANOS
from .dtypes cimport c_Resolution
from .nattype cimport (
NPY_NAT,
c_NaT as NaT,
)
from .np_datetime cimport (
dt64_to_dtstruct,
npy_datetimestruct,
)
from .offsets cimport BaseOffset
from .period cimport get_period_ordinal
from .timestamps cimport (
create_timestamp_from_ts,
normalize_i8_stamp,
)
from .timezones cimport (
get_dst_info,
is_tzlocal,
is_utc,
)
from .tzconversion cimport (
bisect_right_i8,
localize_tzinfo_api,
)
# -------------------------------------------------------------------------
@cython.wraparound(False)
@cython.boundscheck(False)
def ints_to_pydatetime(
const int64_t[:] stamps,
tzinfo tz=None,
BaseOffset freq=None,
bint fold=False,
str box="datetime"
) -> np.ndarray:
"""
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp.
Parameters
----------
stamps : array of i8
tz : str, optional
convert to this timezone
freq : BaseOffset, optional
freq to convert
fold : bint, default is 0
Due to daylight saving time, one wall clock time can occur twice
when shifting from summer to winter time; fold describes whether the
datetime-like corresponds to the first (0) or the second time (1)
the wall clock hits the ambiguous time
.. versionadded:: 1.1.0
box : {'datetime', 'timestamp', 'date', 'time'}, default 'datetime'
* If datetime, convert to datetime.datetime
* If date, convert to datetime.date
* If time, convert to datetime.time
* If Timestamp, convert to pandas.Timestamp
Returns
-------
ndarray[object] of type specified by box
"""
cdef:
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
ndarray[int64_t] trans
int64_t[::1] deltas
int64_t* tdata = NULL
intp_t pos
int64_t utc_val, local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False
str typ
npy_datetimestruct dts
tzinfo new_tz
ndarray[object] result = np.empty(n, dtype=object)
bint use_pytz = False
bint use_date = False, use_time = False, use_ts = False, use_pydt = False
if box == "date":
assert (tz is None), "tz should be None when converting to date"
use_date = True
elif box == "timestamp":
use_ts = True
elif box == "time":
use_time = True
elif box == "datetime":
use_pydt = True
else:
raise ValueError(
"box must be one of 'datetime', 'date', 'time' or 'timestamp'"
)
if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)
ntrans = trans.shape[0]
if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
else:
tdata = <int64_t*>cnp.PyArray_DATA(trans)
use_pytz = typ == "pytz"
for i in range(n):
utc_val = stamps[i]
new_tz = tz
if utc_val == NPY_NAT:
result[i] = <object>NaT
continue
if use_utc:
local_val = utc_val
elif use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif use_fixed:
local_val = utc_val + delta
else:
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
local_val = utc_val + deltas[pos]
if use_pytz:
# find right representation of dst etc in pytz timezone
new_tz = tz._tzinfos[tz._transition_info[pos]]
dt64_to_dtstruct(local_val, &dts)
if use_ts:
result[i] = create_timestamp_from_ts(utc_val, dts, new_tz, freq, fold)
elif use_pydt:
result[i] = datetime(
dts.year, dts.month, dts.day, dts.hour, dts.min, dts.sec, dts.us,
new_tz, fold=fold,
)
elif use_date:
result[i] = date(dts.year, dts.month, dts.day)
else:
result[i] = time(dts.hour, dts.min, dts.sec, dts.us, new_tz, fold=fold)
return result
# -------------------------------------------------------------------------
cdef inline c_Resolution _reso_stamp(npy_datetimestruct *dts):
if dts.us != 0:
if dts.us % 1000 == 0:
return c_Resolution.RESO_MS
return c_Resolution.RESO_US
elif dts.sec != 0:
return c_Resolution.RESO_SEC
elif dts.min != 0:
return c_Resolution.RESO_MIN
elif dts.hour != 0:
return c_Resolution.RESO_HR
return c_Resolution.RESO_DAY
@cython.wraparound(False)
@cython.boundscheck(False)
def get_resolution(const int64_t[:] stamps, tzinfo tz=None) -> Resolution:
cdef:
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
ndarray[int64_t] trans
int64_t[::1] deltas
int64_t* tdata = NULL
intp_t pos
int64_t utc_val, local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False
str typ
npy_datetimestruct dts
c_Resolution reso = c_Resolution.RESO_DAY, curr_reso
if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)
ntrans = trans.shape[0]
if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
else:
tdata = <int64_t*>cnp.PyArray_DATA(trans)
for i in range(n):
utc_val = stamps[i]
if utc_val == NPY_NAT:
continue
if use_utc:
local_val = utc_val
elif use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif use_fixed:
local_val = utc_val + delta
else:
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
local_val = utc_val + deltas[pos]
dt64_to_dtstruct(local_val, &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
return Resolution(reso)
# -------------------------------------------------------------------------
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo tz):
"""
Normalize each of the (nanosecond) timezone aware timestamps in the given
array by rounding down to the beginning of the day (i.e. midnight).
This is midnight for timezone, `tz`.
Parameters
----------
stamps : int64 ndarray
tz : tzinfo or None
Returns
-------
result : int64 ndarray of converted of normalized nanosecond timestamps
"""
cdef:
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
ndarray[int64_t] trans
int64_t[::1] deltas
int64_t* tdata = NULL
intp_t pos
int64_t utc_val, local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False
str typ
int64_t[::1] result = np.empty(n, dtype=np.int64)
if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)
ntrans = trans.shape[0]
if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
else:
tdata = <int64_t*>cnp.PyArray_DATA(trans)
for i in range(n):
utc_val = stamps[i]
if utc_val == NPY_NAT:
result[i] = NPY_NAT
continue
if use_utc:
local_val = utc_val
elif use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif use_fixed:
local_val = utc_val + delta
else:
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
local_val = utc_val + deltas[pos]
result[i] = normalize_i8_stamp(local_val)
return result.base # `.base` to access underlying ndarray
@cython.wraparound(False)
@cython.boundscheck(False)
def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None) -> bool:
"""
Check if all of the given (nanosecond) timestamps are normalized to
midnight, i.e. hour == minute == second == 0. If the optional timezone
`tz` is not None, then this is midnight for this timezone.
Parameters
----------
stamps : int64 ndarray
tz : tzinfo or None
Returns
-------
is_normalized : bool True if all stamps are normalized
"""
cdef:
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
ndarray[int64_t] trans
int64_t[::1] deltas
int64_t* tdata = NULL
intp_t pos
int64_t utc_val, local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False
str typ
if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)
ntrans = trans.shape[0]
if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
else:
tdata = <int64_t*>cnp.PyArray_DATA(trans)
for i in range(n):
utc_val = stamps[i]
if use_utc:
local_val = utc_val
elif use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif use_fixed:
local_val = utc_val + delta
else:
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
local_val = utc_val + deltas[pos]
if local_val % DAY_NANOS != 0:
return False
return True
# -------------------------------------------------------------------------
@cython.wraparound(False)
@cython.boundscheck(False)
def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, tzinfo tz):
cdef:
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
ndarray[int64_t] trans
int64_t[::1] deltas
int64_t* tdata = NULL
intp_t pos
int64_t utc_val, local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False
str typ
npy_datetimestruct dts
int64_t[::1] result = np.empty(n, dtype=np.int64)
if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)
ntrans = trans.shape[0]
if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
else:
tdata = <int64_t*>cnp.PyArray_DATA(trans)
for i in range(n):
utc_val = stamps[i]
if utc_val == NPY_NAT:
result[i] = NPY_NAT
continue
if use_utc:
local_val = utc_val
elif use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif use_fixed:
local_val = utc_val + delta
else:
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
local_val = utc_val + deltas[pos]
dt64_to_dtstruct(local_val, &dts)
result[i] = get_period_ordinal(&dts, freq)
return result.base # .base to get underlying ndarray