forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtslib.pyx
758 lines (639 loc) · 26.1 KB
/
tslib.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# -*- coding: utf-8 -*-
# cython: profile=False
cimport numpy as cnp
from numpy cimport int64_t, ndarray, float64_t
import numpy as np
cnp.import_array()
from cpython cimport PyFloat_Check, PyUnicode_Check
from util cimport (is_integer_object, is_float_object, is_string_object,
is_datetime64_object)
from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
PyDateTime_CheckExact,
PyDateTime_IMPORT,
timedelta, datetime, date, time)
# import datetime C API
PyDateTime_IMPORT
from tslibs.np_datetime cimport (check_dts_bounds,
pandas_datetimestruct,
_string_to_dts,
dt64_to_dtstruct, dtstruct_to_dt64,
pydatetime_to_dt64, pydate_to_dt64,
get_datetime64_value)
from tslibs.np_datetime import OutOfBoundsDatetime
from tslibs.parsing import parse_datetime_string
cimport cython
from cython cimport Py_ssize_t
import pytz
from tslibs.timedeltas cimport cast_from_unit
from tslibs.timezones cimport (is_utc, is_tzlocal, is_fixed_offset,
treat_tz_as_pytz, get_dst_info)
from tslibs.conversion cimport (tz_convert_single, _TSObject,
convert_datetime_to_tsobject,
get_datetime64_nanos,
tz_convert_utc_to_tzlocal)
from tslibs.nattype import NaT, nat_strings, iNaT
from tslibs.nattype cimport checknull_with_nat, NPY_NAT
from tslibs.offsets cimport to_offset
from tslibs.timestamps cimport (create_timestamp_from_ts,
_NS_UPPER_BOUND, _NS_LOWER_BOUND)
from tslibs.timestamps import Timestamp
cdef bint PY2 = str == bytes
cdef inline object create_datetime_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a datetime.datetime from its parts """
return datetime(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, tz)
cdef inline object create_date_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a datetime.date from its parts """
return date(dts.year, dts.month, dts.day)
cdef inline object create_time_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a datetime.time from its parts """
return time(dts.hour, dts.min, dts.sec, dts.us)
def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
box="datetime"):
"""
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp
Parameters
----------
arr : array of i8
tz : str, default None
convert to this timezone
freq : str/Offset, default None
freq to convert
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
-------
result : array of dtype specified by box
"""
cdef:
Py_ssize_t i, n = len(arr)
ndarray[int64_t] trans, deltas
pandas_datetimestruct dts
object dt
int64_t value
ndarray[object] result = np.empty(n, dtype=object)
object (*func_create)(int64_t, pandas_datetimestruct, object, object)
if box == "date":
assert (tz is None), "tz should be None when converting to date"
func_create = create_date_from_ts
elif box == "timestamp":
func_create = create_timestamp_from_ts
if is_string_object(freq):
freq = to_offset(freq)
elif box == "time":
func_create = create_time_from_ts
elif box == "datetime":
func_create = create_datetime_from_ts
else:
raise ValueError("box must be one of 'datetime', 'date', 'time' or"
" 'timestamp'")
if tz is not None:
if is_utc(tz):
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
dt64_to_dtstruct(value, &dts)
result[i] = func_create(value, dts, tz, freq)
elif is_tzlocal(tz) or is_fixed_offset(tz):
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
# Python datetime objects do not support nanosecond
# resolution (yet, PEP 564). Need to compute new value
# using the i8 representation.
local_value = tz_convert_utc_to_tzlocal(value, tz)
dt64_to_dtstruct(local_value, &dts)
result[i] = func_create(value, dts, tz, freq)
else:
trans, deltas, typ = get_dst_info(tz)
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
# Adjust datetime64 timestamp, recompute datetimestruct
pos = trans.searchsorted(value, side='right') - 1
if treat_tz_as_pytz(tz):
# find right representation of dst etc in pytz timezone
new_tz = tz._tzinfos[tz._transition_info[pos]]
else:
# no zone-name change for dateutil tzs - dst etc
# represented in single object.
new_tz = tz
dt64_to_dtstruct(value + deltas[pos], &dts)
result[i] = func_create(value, dts, new_tz, freq)
else:
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
dt64_to_dtstruct(value, &dts)
result[i] = func_create(value, dts, None, freq)
return result
def _test_parse_iso8601(object ts):
"""
TESTING ONLY: Parse string into Timestamp using iso8601 parser. Used
only for testing, actual construction uses `convert_str_to_tsobject`
"""
cdef:
_TSObject obj
int out_local = 0, out_tzoffset = 0
obj = _TSObject()
if ts == 'now':
return Timestamp.utcnow()
elif ts == 'today':
return Timestamp.now().normalize()
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
obj.value = dtstruct_to_dt64(&obj.dts)
check_dts_bounds(&obj.dts)
if out_local == 1:
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
obj.value = tz_convert_single(obj.value, obj.tzinfo, 'UTC')
return Timestamp(obj.value, tz=obj.tzinfo)
else:
return Timestamp(obj.value)
def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
object format=None, object na_rep=None):
"""
return a np object array of the string formatted values
Parameters
----------
values : a 1-d i8 array
tz : the timezone (or None)
format : optional, default is None
a strftime capable string
na_rep : optional, default is None
a nat format
"""
cdef:
int64_t val, ns, N = len(values)
ndarray[int64_t] consider_values
bint show_ms = 0, show_us = 0, show_ns = 0, basic_format = 0
ndarray[object] result = np.empty(N, dtype=object)
object ts, res
pandas_datetimestruct dts
if na_rep is None:
na_rep = 'NaT'
# if we don't have a format nor tz, then choose
# a format based on precision
basic_format = format is None and tz is None
if basic_format:
consider_values = values[values != NPY_NAT]
show_ns = (consider_values % 1000).any()
if not show_ns:
consider_values //= 1000
show_us = (consider_values % 1000).any()
if not show_ms:
consider_values //= 1000
show_ms = (consider_values % 1000).any()
for i in range(N):
val = values[i]
if val == NPY_NAT:
result[i] = na_rep
elif basic_format:
dt64_to_dtstruct(val, &dts)
res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec)
if show_ns:
ns = dts.ps / 1000
res += '.%.9d' % (ns + 1000 * dts.us)
elif show_us:
res += '.%.6d' % dts.us
elif show_ms:
res += '.%.3d' % (dts.us /1000)
result[i] = res
else:
ts = Timestamp(val, tz=tz)
if format is None:
result[i] = str(ts)
else:
# invalid format string
# requires dates > 1900
try:
result[i] = ts.strftime(format)
except ValueError:
result[i] = str(ts)
return result
cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
"""
convert the ndarray according to the unit
if errors:
- raise: return converted values or raise OutOfBoundsDatetime
if out of range on the conversion or
ValueError for other conversions (e.g. a string)
- ignore: return non-convertible values as the same unit
- coerce: NaT for non-convertibles
"""
cdef:
Py_ssize_t i, j, n=len(values)
int64_t m
ndarray[float64_t] fvalues
ndarray mask
bint is_ignore = errors=='ignore'
bint is_coerce = errors=='coerce'
bint is_raise = errors=='raise'
bint need_to_iterate = True
ndarray[int64_t] iresult
ndarray[object] oresult
assert is_ignore or is_coerce or is_raise
if unit == 'ns':
if issubclass(values.dtype.type, np.integer):
return values.astype('M8[ns]')
return array_to_datetime(values.astype(object), errors=errors)[0]
m = cast_from_unit(None, unit)
if is_raise:
# try a quick conversion to i8
# if we have nulls that are not type-compat
# then need to iterate
try:
iresult = values.astype('i8', casting='same_kind', copy=False)
mask = iresult == iNaT
iresult[mask] = 0
fvalues = iresult.astype('f8') * m
need_to_iterate = False
except:
pass
# check the bounds
if not need_to_iterate:
if ((fvalues < _NS_LOWER_BOUND).any()
or (fvalues > _NS_UPPER_BOUND).any()):
raise OutOfBoundsDatetime(
"cannot convert input with unit '{0}'".format(unit))
result = (iresult * m).astype('M8[ns]')
iresult = result.view('i8')
iresult[mask] = iNaT
return result
result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')
try:
for i in range(n):
val = values[i]
if checknull_with_nat(val):
iresult[i] = NPY_NAT
elif is_integer_object(val) or is_float_object(val):
if val != val or val == NPY_NAT:
iresult[i] = NPY_NAT
else:
try:
iresult[i] = cast_from_unit(val, unit)
except OverflowError:
if is_raise:
raise OutOfBoundsDatetime(
"cannot convert input {0} with the unit "
"'{1}'".format(val, unit))
elif is_ignore:
raise AssertionError
iresult[i] = NPY_NAT
elif is_string_object(val):
if len(val) == 0 or val in nat_strings:
iresult[i] = NPY_NAT
else:
try:
iresult[i] = cast_from_unit(float(val), unit)
except ValueError:
if is_raise:
raise ValueError(
"non convertible value {0} with the unit "
"'{1}'".format(val, unit))
elif is_ignore:
raise AssertionError
iresult[i] = NPY_NAT
except:
if is_raise:
raise OutOfBoundsDatetime(
"cannot convert input {0} with the unit "
"'{1}'".format(val, unit))
elif is_ignore:
raise AssertionError
iresult[i] = NPY_NAT
else:
if is_raise:
raise ValueError("unit='{0}' not valid with non-numerical "
"val='{1}'".format(unit, val))
if is_ignore:
raise AssertionError
iresult[i] = NPY_NAT
return result
except AssertionError:
pass
# we have hit an exception
# and are in ignore mode
# redo as object
oresult = np.empty(n, dtype=object)
for i in range(n):
val = values[i]
if checknull_with_nat(val):
oresult[i] = NaT
elif is_integer_object(val) or is_float_object(val):
if val != val or val == NPY_NAT:
oresult[i] = NaT
else:
try:
oresult[i] = Timestamp(cast_from_unit(val, unit))
except:
oresult[i] = val
elif is_string_object(val):
if len(val) == 0 or val in nat_strings:
oresult[i] = NaT
else:
oresult[i] = val
return oresult
cpdef array_to_datetime(ndarray[object] values, errors='raise',
dayfirst=False, yearfirst=False,
format=None, utc=None,
require_iso8601=False):
"""
Converts a 1D array of date-like values to a numpy array of either:
1) datetime64[ns] data
2) datetime.datetime objects, if OutOfBoundsDatetime or TypeError
is encountered
Also returns a pytz.FixedOffset if an array of strings with the same
timezone offset if passed and utc=True is not passed
Handles datetime.date, datetime.datetime, np.datetime64 objects, numeric,
strings
Returns
-------
(ndarray, timezone offset)
"""
cdef:
Py_ssize_t i, n = len(values)
object val, py_dt, tz, tz_out = None
ndarray[int64_t] iresult
ndarray[object] oresult
pandas_datetimestruct dts
bint utc_convert = bool(utc)
bint seen_integer = 0
bint seen_string = 0
bint seen_datetime = 0
bint seen_datetime_offset = 0
bint is_raise = errors=='raise'
bint is_ignore = errors=='ignore'
bint is_coerce = errors=='coerce'
_TSObject _ts
int out_local=0, out_tzoffset=0
# Can't directly create a ndarray[int] out_local,
# since most np.array constructors expect a long dtype
# while _string_to_dts expects purely int
# maybe something I am missing?
ndarray[int64_t] out_local_values
ndarray[int64_t] out_tzoffset_vals
# specify error conditions
assert is_raise or is_ignore or is_coerce
try:
out_local_values = np.empty(n, dtype=np.int64)
out_tzoffset_vals = np.empty(n, dtype=np.int64)
result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')
for i in range(n):
val = values[i]
if checknull_with_nat(val):
iresult[i] = NPY_NAT
elif PyDateTime_Check(val):
seen_datetime = 1
if val.tzinfo is not None:
if utc_convert:
try:
_ts = convert_datetime_to_tsobject(val, None)
iresult[i] = _ts.value
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
else:
raise ValueError('Tz-aware datetime.datetime cannot '
'be converted to datetime64 unless '
'utc=True')
else:
iresult[i] = pydatetime_to_dt64(val, &dts)
if not PyDateTime_CheckExact(val):
# i.e. a Timestamp object
iresult[i] += val.nanosecond
try:
check_dts_bounds(&dts)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
elif PyDate_Check(val):
seen_datetime = 1
iresult[i] = pydate_to_dt64(val, &dts)
try:
check_dts_bounds(&dts)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
elif is_datetime64_object(val):
seen_datetime = 1
if get_datetime64_value(val) == NPY_NAT:
iresult[i] = NPY_NAT
else:
try:
iresult[i] = get_datetime64_nanos(val)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
elif is_integer_object(val) or is_float_object(val):
# these must be ns unit by-definition
seen_integer = 1
if val != val or val == NPY_NAT:
iresult[i] = NPY_NAT
elif is_raise or is_ignore:
iresult[i] = val
else:
# coerce
# we now need to parse this as if unit='ns'
# we can ONLY accept integers at this point
# if we have previously (or in future accept
# datetimes/strings, then we must coerce)
try:
iresult[i] = cast_from_unit(val, 'ns')
except:
iresult[i] = NPY_NAT
elif is_string_object(val):
# string
seen_string = 1
if len(val) == 0 or val in nat_strings:
iresult[i] = NPY_NAT
continue
if PyUnicode_Check(val) and PY2:
val = val.encode('utf-8')
try:
_string_to_dts(val, &dts, &out_local, &out_tzoffset)
except ValueError:
# A ValueError at this point is a _parsing_ error
# specifically _not_ OutOfBoundsDatetime
if _parse_today_now(val, &iresult[i]):
continue
elif require_iso8601:
# if requiring iso8601 strings, skip trying
# other formats
if is_coerce:
iresult[i] = NPY_NAT
continue
elif is_raise:
raise ValueError("time data {val} doesn't match "
"format specified"
.format(val=val))
return values, tz_out
try:
py_dt = parse_datetime_string(val, dayfirst=dayfirst,
yearfirst=yearfirst)
except Exception:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise TypeError("invalid string coercion to datetime")
try:
_ts = convert_datetime_to_tsobject(py_dt, None)
iresult[i] = _ts.value
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
except:
# TODO: What exception are we concerned with here?
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
else:
# No error raised by string_to_dts, pick back up
# where we left off
out_tzoffset_vals[i] = out_tzoffset
out_local_values[i] = out_local
value = dtstruct_to_dt64(&dts)
if out_local == 1:
seen_datetime_offset = 1
tz = pytz.FixedOffset(out_tzoffset)
value = tz_convert_single(value, tz, 'UTC')
iresult[i] = value
try:
check_dts_bounds(&dts)
except OutOfBoundsDatetime:
# GH#19382 for just-barely-OutOfBounds falling back to
# dateutil parser will return incorrect result because
# it will ignore nanoseconds
if is_coerce:
iresult[i] = NPY_NAT
continue
elif require_iso8601:
if is_raise:
raise ValueError("time data {val} doesn't "
"match format specified"
.format(val=val))
return values, tz_out
raise
else:
if is_coerce:
iresult[i] = NPY_NAT
else:
raise TypeError("{0} is not convertible to datetime"
.format(type(val)))
if seen_datetime and seen_integer:
# we have mixed datetimes & integers
if is_coerce:
# coerce all of the integers/floats to NaT, preserve
# the datetimes and other convertibles
for i in range(n):
val = values[i]
if is_integer_object(val) or is_float_object(val):
result[i] = NPY_NAT
elif is_raise:
raise ValueError(
"mixed datetimes and integers in passed array")
else:
raise TypeError
if seen_datetime_offset and not utc_convert:
# GH 17697
# 1) If all the offsets are equal, return one pytz.FixedOffset for
# the parsed dates to (maybe) pass to DatetimeIndex
# 2) If the offsets are different, then force the parsing down the
# object path where an array of datetimes
# (with individual datutil.tzoffsets) are returned
# Faster to compare integers than to compare objects
is_same_offsets = (out_tzoffset_vals[0] == out_tzoffset_vals).all()
if not is_same_offsets:
raise TypeError
else:
tz_out = pytz.FixedOffset(out_tzoffset_vals[0])
return result, tz_out
except OutOfBoundsDatetime:
if is_raise:
raise
oresult = np.empty(n, dtype=object)
for i in range(n):
val = values[i]
# set as nan except if its a NaT
if checknull_with_nat(val):
if PyFloat_Check(val):
oresult[i] = np.nan
else:
oresult[i] = NaT
elif is_datetime64_object(val):
if get_datetime64_value(val) == NPY_NAT:
oresult[i] = NaT
else:
oresult[i] = val.item()
else:
oresult[i] = val
return oresult, tz_out
except TypeError:
oresult = np.empty(n, dtype=object)
for i in range(n):
val = values[i]
if checknull_with_nat(val):
oresult[i] = val
elif is_string_object(val):
if len(val) == 0 or val in nat_strings:
oresult[i] = 'NaT'
continue
try:
oresult[i] = parse_datetime_string(val, dayfirst=dayfirst,
yearfirst=yearfirst)
pydatetime_to_dt64(oresult[i], &dts)
check_dts_bounds(&dts)
except Exception:
if is_raise:
raise
return values, tz_out
else:
if is_raise:
raise
return values, tz_out
return oresult, tz_out
cdef inline bint _parse_today_now(str val, int64_t* iresult):
# We delay this check for as long as possible
# because it catches relatively rare cases
if val == 'now':
# Note: this is *not* the same as Timestamp('now')
iresult[0] = Timestamp.utcnow().value
return True
elif val == 'today':
iresult[0] = Timestamp.today().value
return True
return False