-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
/
tslib.pyx
5721 lines (4756 loc) · 185 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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# cython: profile=False
import warnings
cimport numpy as np
from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
float64_t,
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
import numpy as np
import sys
cdef bint PY3 = (sys.version_info[0] >= 3)
from cpython cimport (
PyTypeObject,
PyFloat_Check,
PyComplex_Check,
PyLong_Check,
PyObject_RichCompareBool,
PyObject_RichCompare,
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE,
PyUnicode_Check,
PyUnicode_AsUTF8String,
)
cdef extern from "Python.h":
cdef PyTypeObject *Py_TYPE(object)
cdef extern from "datetime_helper.h":
double total_seconds(object)
# this is our datetime.pxd
from libc.stdlib cimport free
from util cimport (is_integer_object, is_float_object, is_datetime64_object,
is_timedelta64_object, INT64_MAX)
cimport util
# this is our datetime.pxd
from datetime cimport (
pandas_datetimestruct,
pandas_datetime_to_datetimestruct,
pandas_datetimestruct_to_datetime,
cmp_pandas_datetimestruct,
days_per_month_table,
get_datetime64_value,
get_timedelta64_value,
get_datetime64_unit,
PANDAS_DATETIMEUNIT,
_string_to_dts,
_pydatetime_to_dts,
_date_to_datetime64,
npy_datetime,
is_leapyear,
dayofweek,
PANDAS_FR_ns,
PyDateTime_Check, PyDate_Check,
PyDateTime_IMPORT,
timedelta, datetime
)
# stdlib datetime imports
from datetime import timedelta, datetime
from datetime import time as datetime_time
from khash cimport (
khiter_t,
kh_destroy_int64, kh_put_int64,
kh_init_int64, kh_int64_t,
kh_resize_int64, kh_get_int64)
cimport cython
import re
# dateutil compat
from dateutil.tz import (tzoffset, tzlocal as _dateutil_tzlocal,
tzfile as _dateutil_tzfile,
tzutc as _dateutil_tzutc,
tzstr as _dateutil_tzstr)
from pandas.compat import is_platform_windows
if is_platform_windows():
from dateutil.zoneinfo import gettz as _dateutil_gettz
else:
from dateutil.tz import gettz as _dateutil_gettz
from dateutil.relativedelta import relativedelta
from dateutil.parser import DEFAULTPARSER
from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo
from pandas.compat import (parse_date, string_types, iteritems,
StringIO, callable)
import operator
import collections
import warnings
# initialize numpy
import_array()
#import_ufunc()
# import datetime C API
PyDateTime_IMPORT
# in numpy 1.7, will prob need the following:
# numpy_pydatetime_import
cdef int64_t NPY_NAT = util.get_nat()
iNaT = NPY_NAT
cdef inline object create_timestamp_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a Timestamp from its parts """
cdef _Timestamp ts_base
ts_base = _Timestamp.__new__(Timestamp, dts.year, dts.month,
dts.day, dts.hour, dts.min,
dts.sec, dts.us, tz)
ts_base.value = value
ts_base.freq = freq
ts_base.nanosecond = dts.ps / 1000
return ts_base
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)
def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
# convert an i8 repr to an ndarray of datetimes or Timestamp (if box ==
# True)
cdef:
Py_ssize_t i, n = len(arr)
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 and util.is_string_object(freq):
from pandas.tseries.frequencies import to_offset
freq = to_offset(freq)
if box:
func_create = create_timestamp_from_ts
else:
func_create = create_datetime_from_ts
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:
pandas_datetime_to_datetimestruct(
value, PANDAS_FR_ns, &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:
pandas_datetime_to_datetimestruct(
value, PANDAS_FR_ns, &dts)
dt = create_datetime_from_ts(value, dts, tz, freq)
dt = dt + tz.utcoffset(dt)
if box:
dt = Timestamp(dt)
result[i] = dt
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
pandas_datetime_to_datetimestruct(
value + deltas[pos], PANDAS_FR_ns, &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:
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
result[i] = func_create(value, dts, None, freq)
return result
def ints_to_pytimedelta(ndarray[int64_t] arr, box=False):
# convert an i8 repr to an ndarray of timedelta or Timedelta (if box ==
# True)
cdef:
Py_ssize_t i, n = len(arr)
int64_t value
ndarray[object] result = np.empty(n, dtype=object)
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
if box:
result[i] = Timedelta(value)
else:
result[i] = timedelta(microseconds=int(value) / 1000)
return result
cdef inline bint _is_tzlocal(object tz):
return isinstance(tz, _dateutil_tzlocal)
cdef inline bint _is_fixed_offset(object tz):
if _treat_tz_as_dateutil(tz):
if len(tz._trans_idx) == 0 and len(tz._trans_list) == 0:
return 1
else:
return 0
elif _treat_tz_as_pytz(tz):
if (len(tz._transition_info) == 0
and len(tz._utc_transition_times) == 0):
return 1
else:
return 0
return 1
_zero_time = datetime_time(0, 0)
_no_input = object()
# Python front end to C extension type _Timestamp
# This serves as the box for datetime64
class Timestamp(_Timestamp):
"""TimeStamp is the pandas equivalent of python's Datetime
and is interchangable with it in most cases. It's the type used
for the entries that make up a DatetimeIndex, and other timeseries
oriented data structures in pandas.
There are essentially three calling conventions for the constructor. The
primary form accepts four parameters. They can be passed by position or
keyword.
Parameters
----------
ts_input : datetime-like, str, int, float
Value to be converted to Timestamp
freq : str, DateOffset
Offset which Timestamp will have
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
unit : string
numpy unit used for conversion, if ts_input is int or float
offset : str, DateOffset
Deprecated, use freq
The other two forms mimic the parameters from ``datetime.datetime``. They
can be passed by either position or keyword, but not both mixed together.
:func:`datetime.datetime` Parameters
------------------------------------
.. versionadded:: 0.19.0
year : int
month : int
day : int
hour : int, optional, default is 0
minute : int, optional, default is 0
second : int, optional, default is 0
microsecond : int, optional, default is 0
tzinfo : datetime.tzinfo, optional, default is None
"""
@classmethod
def fromordinal(cls, ordinal, freq=None, tz=None, offset=None):
"""
passed an ordinal, translate and convert to a ts
note: by definition there cannot be any tz info on the ordinal itself
Parameters
----------
ordinal : int
date corresponding to a proleptic Gregorian ordinal
freq : str, DateOffset
Offset which Timestamp will have
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
offset : str, DateOffset
Deprecated, use freq
"""
return cls(datetime.fromordinal(ordinal),
freq=freq, tz=tz, offset=offset)
@classmethod
def now(cls, tz=None):
"""
Return the current time in the local timezone. Equivalent
to datetime.now([tz])
Parameters
----------
tz : string / timezone object, default None
Timezone to localize to
"""
if isinstance(tz, string_types):
tz = maybe_get_tz(tz)
return cls(datetime.now(tz))
@classmethod
def today(cls, tz=None):
"""
Return the current time in the local timezone. This differs
from datetime.today() in that it can be localized to a
passed timezone.
Parameters
----------
tz : string / timezone object, default None
Timezone to localize to
"""
return cls.now(tz)
@classmethod
def utcnow(cls):
return cls.now('UTC')
@classmethod
def utcfromtimestamp(cls, ts):
return cls(datetime.utcfromtimestamp(ts))
@classmethod
def fromtimestamp(cls, ts):
return cls(datetime.fromtimestamp(ts))
@classmethod
def combine(cls, date, time):
return cls(datetime.combine(date, time))
def __new__(cls, object ts_input=_no_input,
object freq=None, tz=None, unit=None,
year=None, month=None, day=None,
hour=None, minute=None, second=None, microsecond=None,
tzinfo=None,
object offset=None):
# The parameter list folds together legacy parameter names (the first
# four) and positional and keyword parameter names from pydatetime.
#
# There are three calling forms:
#
# - In the legacy form, the first parameter, ts_input, is required
# and may be datetime-like, str, int, or float. The second
# parameter, offset, is optional and may be str or DateOffset.
#
# - ints in the first, second, and third arguments indicate
# pydatetime positional arguments. Only the first 8 arguments
# (standing in for year, month, day, hour, minute, second,
# microsecond, tzinfo) may be non-None. As a shortcut, we just
# check that the second argument is an int.
#
# - Nones for the first four (legacy) arguments indicate pydatetime
# keyword arguments. year, month, and day are required. As a
# shortcut, we just check that the first argument was not passed.
#
# Mixing pydatetime positional and keyword arguments is forbidden!
cdef _TSObject ts
if offset is not None:
# deprecate offset kwd in 0.19.0, GH13593
if freq is not None:
msg = "Can only specify freq or offset, not both"
raise TypeError(msg)
warnings.warn("offset is deprecated. Use freq instead",
FutureWarning)
freq = offset
if ts_input is _no_input:
# User passed keyword arguments.
return Timestamp(datetime(year, month, day, hour or 0,
minute or 0, second or 0,
microsecond or 0, tzinfo),
tz=tzinfo)
elif is_integer_object(freq):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
# microsecond[, tzinfo]]]]])
return Timestamp(datetime(ts_input, freq, tz, unit or 0,
year or 0, month or 0, day or 0,
hour), tz=hour)
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
if ts.value == NPY_NAT:
return NaT
if util.is_string_object(freq):
from pandas.tseries.frequencies import to_offset
freq = to_offset(freq)
return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, freq)
def _round(self, freq, rounder):
cdef int64_t unit
cdef object result, value
from pandas.tseries.frequencies import to_offset
unit = to_offset(freq).nanos
if self.tz is not None:
value = self.tz_localize(None).value
else:
value = self.value
if unit < 1000 and unit % 1000 != 0:
# for nano rounding, work with the last 6 digits separately
# due to float precision
buff = 1000000
result = (buff * (value // buff) + unit *
(rounder((value % buff) / float(unit))).astype('i8'))
elif unit >= 1000 and unit % 1000 != 0:
msg = 'Precision will be lost using frequency: {}'
warnings.warn(msg.format(freq))
result = (unit * rounder(value / float(unit)).astype('i8'))
else:
result = (unit * rounder(value / float(unit)).astype('i8'))
result = Timestamp(result, unit='ns')
if self.tz is not None:
result = result.tz_localize(self.tz)
return result
def round(self, freq):
"""
Round the Timestamp to the specified resolution
Returns
-------
a new Timestamp rounded to the given resolution of `freq`
Parameters
----------
freq : a freq string indicating the rounding resolution
Raises
------
ValueError if the freq cannot be converted
"""
return self._round(freq, np.round)
def floor(self, freq):
"""
return a new Timestamp floored to this resolution
Parameters
----------
freq : a freq string indicating the flooring resolution
"""
return self._round(freq, np.floor)
def ceil(self, freq):
"""
return a new Timestamp ceiled to this resolution
Parameters
----------
freq : a freq string indicating the ceiling resolution
"""
return self._round(freq, np.ceil)
@property
def tz(self):
"""
Alias for tzinfo
"""
return self.tzinfo
@property
def offset(self):
warnings.warn(".offset is deprecated. Use .freq instead",
FutureWarning)
return self.freq
def __setstate__(self, state):
self.value = state[0]
self.freq = state[1]
self.tzinfo = state[2]
def __reduce__(self):
object_state = self.value, self.freq, self.tzinfo
return (Timestamp, object_state)
def to_period(self, freq=None):
"""
Return an period of which this timestamp is an observation.
"""
from pandas import Period
if freq is None:
freq = self.freq
return Period(self, freq=freq)
@property
def dayofweek(self):
return self.weekday()
@property
def weekday_name(self):
out = get_date_name_field(
np.array([self.value], dtype=np.int64), 'weekday_name')
return out[0]
@property
def dayofyear(self):
return self._get_field('doy')
@property
def week(self):
return self._get_field('woy')
weekofyear = week
@property
def microsecond(self):
return self._get_field('us')
@property
def quarter(self):
return self._get_field('q')
@property
def days_in_month(self):
return self._get_field('dim')
daysinmonth = days_in_month
@property
def freqstr(self):
return getattr(self.freq, 'freqstr', self.freq)
@property
def is_month_start(self):
return self._get_start_end_field('is_month_start')
@property
def is_month_end(self):
return self._get_start_end_field('is_month_end')
@property
def is_quarter_start(self):
return self._get_start_end_field('is_quarter_start')
@property
def is_quarter_end(self):
return self._get_start_end_field('is_quarter_end')
@property
def is_year_start(self):
return self._get_start_end_field('is_year_start')
@property
def is_year_end(self):
return self._get_start_end_field('is_year_end')
@property
def is_leap_year(self):
return bool(is_leapyear(self.year))
def tz_localize(self, tz, ambiguous='raise', errors='raise'):
"""
Convert naive Timestamp to local time zone, or remove
timezone from tz-aware Timestamp.
Parameters
----------
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will be converted to.
None will remove timezone holding local time.
ambiguous : bool, 'NaT', default 'raise'
- bool contains flags to determine if time is dst or not (note
that this flag is only applicable for ambiguous fall dst dates)
- 'NaT' will return NaT for an ambiguous time
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
errors : 'raise', 'coerce', default 'raise'
- 'raise' will raise a NonExistentTimeError if a timestamp is not
valid in the specified timezone (e.g. due to a transition from
or to DST time)
- 'coerce' will return NaT if the timestamp can not be converted
into the specified timezone
.. versionadded:: 0.19.0
Returns
-------
localized : Timestamp
Raises
------
TypeError
If the Timestamp is tz-aware and tz is not None.
"""
if ambiguous == 'infer':
raise ValueError('Cannot infer offset with only one time.')
if self.tzinfo is None:
# tz naive, localize
tz = maybe_get_tz(tz)
if not isinstance(ambiguous, string_types):
ambiguous = [ambiguous]
value = tz_localize_to_utc(np.array([self.value], dtype='i8'), tz,
ambiguous=ambiguous, errors=errors)[0]
return Timestamp(value, tz=tz)
else:
if tz is None:
# reset tz
value = tz_convert_single(self.value, 'UTC', self.tz)
return Timestamp(value, tz=None)
else:
raise TypeError('Cannot localize tz-aware Timestamp, use '
'tz_convert for conversions')
def tz_convert(self, tz):
"""
Convert tz-aware Timestamp to another time zone.
Parameters
----------
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will be converted to.
None will remove timezone holding UTC time.
Returns
-------
converted : Timestamp
Raises
------
TypeError
If Timestamp is tz-naive.
"""
if self.tzinfo is None:
# tz naive, use tz_localize
raise TypeError('Cannot convert tz-naive Timestamp, use '
'tz_localize to localize')
else:
# Same UTC timestamp, different time zone
return Timestamp(self.value, tz=tz)
astimezone = tz_convert
def replace(self, year=None, month=None, day=None,
hour=None, minute=None, second=None, microsecond=None,
nanosecond=None, tzinfo=object, fold=0):
"""
implements datetime.replace, handles nanoseconds
Parameters
----------
year : int, optional
month : int, optional
day : int, optional
hour : int, optional
minute : int, optional
second : int, optional
microsecond : int, optional
nanosecond: int, optional
tzinfo : tz-convertible, optional
fold : int, optional, default is 0
added in 3.6, NotImplemented
Returns
-------
Timestamp with fields replaced
"""
cdef:
pandas_datetimestruct dts
int64_t value
object _tzinfo, result, k, v
# set to naive if needed
_tzinfo = self.tzinfo
value = self.value
if _tzinfo is not None:
value = tz_convert_single(value, 'UTC', _tzinfo)
# setup components
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
dts.ps = self.nanosecond * 1000
# replace
def validate(k, v):
""" validate integers """
if not is_integer_object(v):
raise ValueError("value must be an integer, received "
"{v} for {k}".format(v=type(v), k=k))
return v
if year is not None:
dts.year = validate('year', year)
if month is not None:
dts.month = validate('month', month)
if day is not None:
dts.day = validate('day', day)
if hour is not None:
dts.hour = validate('hour', hour)
if minute is not None:
dts.min = validate('minute', minute)
if second is not None:
dts.sec = validate('second', second)
if microsecond is not None:
dts.us = validate('microsecond', microsecond)
if nanosecond is not None:
dts.ps = validate('nanosecond', nanosecond) * 1000
if tzinfo is not object:
_tzinfo = tzinfo
# reconstruct & check bounds
value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
if value != NPY_NAT:
_check_dts_bounds(&dts)
# set tz if needed
if _tzinfo is not None:
value = tz_convert_single(value, _tzinfo, 'UTC')
result = create_timestamp_from_ts(value, dts, _tzinfo, self.freq)
return result
def isoformat(self, sep='T'):
base = super(_Timestamp, self).isoformat(sep=sep)
if self.nanosecond == 0:
return base
if self.tzinfo is not None:
base1, base2 = base[:-6], base[-6:]
else:
base1, base2 = base, ""
if self.microsecond != 0:
base1 += "%.3d" % self.nanosecond
else:
base1 += ".%.9d" % self.nanosecond
return base1 + base2
def _has_time_component(self):
"""
Returns if the Timestamp has a time component
in addition to the date part
"""
return (self.time() != _zero_time
or self.tzinfo is not None
or self.nanosecond != 0)
def to_julian_date(self):
"""
Convert TimeStamp to a Julian Date.
0 Julian date is noon January 1, 4713 BC.
"""
year = self.year
month = self.month
day = self.day
if month <= 2:
year -= 1
month += 12
return (day +
np.fix((153 * month - 457) / 5) +
365 * year +
np.floor(year / 4) -
np.floor(year / 100) +
np.floor(year / 400) +
1721118.5 +
(self.hour +
self.minute / 60.0 +
self.second / 3600.0 +
self.microsecond / 3600.0 / 1e+6 +
self.nanosecond / 3600.0 / 1e+9
) / 24.0)
def normalize(self):
"""
Normalize Timestamp to midnight, preserving
tz information.
"""
normalized_value = date_normalize(
np.array([self.value], dtype='i8'), tz=self.tz)[0]
return Timestamp(normalized_value).tz_localize(self.tz)
def __radd__(self, other):
# __radd__ on cython extension types like _Timestamp is not used, so
# define it here instead
return self + other
_nat_strings = set(['NaT', 'nat', 'NAT', 'nan', 'NaN', 'NAN'])
class NaTType(_NaT):
"""(N)ot-(A)-(T)ime, the time equivalent of NaN"""
def __new__(cls):
cdef _NaT base
base = _NaT.__new__(cls, 1, 1, 1)
base._day = -1
base._month = -1
base.value = NPY_NAT
return base
def __repr__(self):
return 'NaT'
def __str__(self):
return 'NaT'
def isoformat(self, sep='T'):
# This allows Timestamp(ts.isoformat()) to always correctly roundtrip.
return 'NaT'
def __hash__(self):
return NPY_NAT
def __int__(self):
return NPY_NAT
def __long__(self):
return NPY_NAT
def __reduce__(self):
return (__nat_unpickle, (None, ))
def total_seconds(self):
# GH 10939
return np.nan
@property
def is_leap_year(self):
return False
@property
def is_month_start(self):
return False
@property
def is_quarter_start(self):
return False
@property
def is_year_start(self):
return False
@property
def is_month_end(self):
return False
@property
def is_quarter_end(self):
return False
@property
def is_year_end(self):
return False
def __rdiv__(self, other):
return _nat_rdivide_op(self, other)
def __rtruediv__(self, other):
return _nat_rdivide_op(self, other)
def __rfloordiv__(self, other):
return _nat_rdivide_op(self, other)
def __rmul__(self, other):
if is_integer_object(other) or is_float_object(other):
return NaT
return NotImplemented
def __nat_unpickle(*args):
# return constant defined in the module
return NaT
NaT = NaTType()
cdef inline bint _checknull_with_nat(object val):
""" utility to check if a value is a nat or not """
return val is None or (
PyFloat_Check(val) and val != val) or val is NaT
cdef inline bint _check_all_nulls(object val):
""" utility to check if a value is any type of null """
cdef bint res
if PyFloat_Check(val) or PyComplex_Check(val):
res = val != val
elif val is NaT:
res = 1
elif val is None:
res = 1
elif is_datetime64_object(val):
res = get_datetime64_value(val) == NPY_NAT
elif is_timedelta64_object(val):
res = get_timedelta64_value(val) == NPY_NAT
else:
res = 0
return res
cdef inline bint _cmp_nat_dt(_NaT lhs, _Timestamp rhs, int op) except -1:
return _nat_scalar_rules[op]
cpdef object get_value_box(ndarray arr, object loc):
cdef:
Py_ssize_t i, sz
void* data_ptr
if util.is_float_object(loc):
casted = int(loc)
if casted == loc:
loc = casted
i = <Py_ssize_t> loc
sz = np.PyArray_SIZE(arr)
if i < 0 and sz > 0:
i += sz
if i >= sz or sz == 0 or i < 0:
raise IndexError('index out of bounds')
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_1d(arr, i))
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_1d(arr, i))
else:
return util.get_value_1d(arr, i)
# Add the min and max fields at the class level
cdef int64_t _NS_UPPER_BOUND = INT64_MAX
# the smallest value we could actually represent is
# INT64_MIN + 1 == -9223372036854775807
# but to allow overflow free conversion with a microsecond resolution
# use the smallest value with a 0 nanosecond unit (0s in last 3 digits)
cdef int64_t _NS_LOWER_BOUND = -9223372036854775000
cdef pandas_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS
pandas_datetime_to_datetimestruct(_NS_LOWER_BOUND, PANDAS_FR_ns, &_NS_MIN_DTS)
pandas_datetime_to_datetimestruct(_NS_UPPER_BOUND, PANDAS_FR_ns, &_NS_MAX_DTS)
# Resolution is in nanoseconds
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
#----------------------------------------------------------------------
# Frequency inference
def unique_deltas(ndarray[int64_t] arr):
cdef:
Py_ssize_t i, n = len(arr)
int64_t val
khiter_t k
kh_int64_t *table
int ret = 0
list uniques = []
table = kh_init_int64()
kh_resize_int64(table, 10)
for i in range(n - 1):
val = arr[i + 1] - arr[i]
k = kh_get_int64(table, val)