forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimestamps.pyx
1329 lines (1097 loc) · 44.9 KB
/
timestamps.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
# -*- coding: utf-8 -*-
import warnings
from cpython cimport (PyObject_RichCompareBool, PyObject_RichCompare,
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE)
import numpy as np
cimport numpy as cnp
from numpy cimport int64_t, int32_t, int8_t
cnp.import_array()
from datetime import time as datetime_time
from cpython.datetime cimport (datetime,
PyDateTime_Check, PyDelta_Check, PyTZInfo_Check,
PyDateTime_IMPORT)
PyDateTime_IMPORT
from util cimport (is_datetime64_object, is_timedelta64_object,
is_integer_object, is_string_object, is_array,
is_offset_object)
cimport ccalendar
from ccalendar import DAY_SECONDS
from conversion import tz_localize_to_utc, normalize_i8_timestamps
from conversion cimport (tz_convert_single, _TSObject,
convert_to_tsobject, convert_datetime_to_tsobject)
from fields import get_start_end_field, get_date_name_field
from nattype cimport NPY_NAT, c_NaT as NaT
from np_datetime import OutOfBoundsDatetime
from np_datetime cimport (reverse_ops, cmp_scalar, check_dts_bounds,
npy_datetimestruct, dt64_to_dtstruct)
from offsets cimport to_offset
from timedeltas import Timedelta
from timedeltas cimport delta_to_nanoseconds
from timezones cimport (
get_timezone, is_utc, maybe_get_tz, treat_tz_as_pytz, tz_compare)
from timezones import UTC
# ----------------------------------------------------------------------
# Constants
_zero_time = datetime_time(0, 0)
_no_input = object()
# ----------------------------------------------------------------------
def maybe_integer_op_deprecated(obj):
# GH#22535 add/sub of integers and int-arrays is deprecated
if obj.freq is not None:
warnings.warn("Addition/subtraction of integers and integer-arrays "
"to {cls} is deprecated, will be removed in a future "
"version. Instead of adding/subtracting `n`, use "
"`n * self.freq`"
.format(cls=type(obj).__name__),
FutureWarning)
cdef inline object create_timestamp_from_ts(int64_t value,
npy_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
class RoundTo(object):
"""
enumeration defining the available rounding modes
Attributes
----------
MINUS_INFTY
round towards -∞, or floor [2]_
PLUS_INFTY
round towards +∞, or ceil [3]_
NEAREST_HALF_EVEN
round to nearest, tie-break half to even [6]_
NEAREST_HALF_MINUS_INFTY
round to nearest, tie-break half to -∞ [5]_
NEAREST_HALF_PLUS_INFTY
round to nearest, tie-break half to +∞ [4]_
References
----------
.. [1] "Rounding - Wikipedia"
https://en.wikipedia.org/wiki/Rounding
.. [2] "Rounding down"
https://en.wikipedia.org/wiki/Rounding#Rounding_down
.. [3] "Rounding up"
https://en.wikipedia.org/wiki/Rounding#Rounding_up
.. [4] "Round half up"
https://en.wikipedia.org/wiki/Rounding#Round_half_up
.. [5] "Round half down"
https://en.wikipedia.org/wiki/Rounding#Round_half_down
.. [6] "Round half to even"
https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
"""
@property
def MINUS_INFTY(self):
return 0
@property
def PLUS_INFTY(self):
return 1
@property
def NEAREST_HALF_EVEN(self):
return 2
@property
def NEAREST_HALF_PLUS_INFTY(self):
return 3
@property
def NEAREST_HALF_MINUS_INFTY(self):
return 4
cdef inline _npdivmod(x1, x2):
"""implement divmod for numpy < 1.13"""
return np.floor_divide(x1, x2), np.remainder(x1, x2)
try:
from numpy import divmod as npdivmod
except ImportError:
# numpy < 1.13
npdivmod = _npdivmod
cdef inline _floor_int64(values, unit):
return values - np.remainder(values, unit)
cdef inline _ceil_int64(values, unit):
return values + np.remainder(-values, unit)
cdef inline _rounddown_int64(values, unit):
return _ceil_int64(values - unit//2, unit)
cdef inline _roundup_int64(values, unit):
return _floor_int64(values + unit//2, unit)
def round_nsint64(values, mode, freq):
"""
Applies rounding mode at given frequency
Parameters
----------
values : :obj:`ndarray`
mode : instance of `RoundTo` enumeration
freq : str, obj
Returns
-------
:obj:`ndarray`
"""
unit = to_offset(freq).nanos
if mode == RoundTo.MINUS_INFTY:
return _floor_int64(values, unit)
elif mode == RoundTo.PLUS_INFTY:
return _ceil_int64(values, unit)
elif mode == RoundTo.NEAREST_HALF_MINUS_INFTY:
return _rounddown_int64(values, unit)
elif mode == RoundTo.NEAREST_HALF_PLUS_INFTY:
return _roundup_int64(values, unit)
elif mode == RoundTo.NEAREST_HALF_EVEN:
# for odd unit there is no need of a tie break
if unit % 2:
return _rounddown_int64(values, unit)
quotient, remainder = npdivmod(values, unit)
mask = np.logical_or(
remainder > (unit // 2),
np.logical_and(remainder == (unit // 2), quotient % 2)
)
quotient[mask] += 1
return quotient * unit
# if/elif above should catch all rounding modes defined in enum 'RoundTo':
# if flow of control arrives here, it is a bug
raise ValueError("round_nsint64 called with an unrecognized "
"rounding mode")
# This is PITA. Because we inherit from datetime, which has very specific
# construction requirements, we need to do object instantiation in python
# (see Timestamp class above). This will serve as a C extension type that
# shadows the python class, where we do any heavy lifting.
cdef class _Timestamp(datetime):
cdef readonly:
int64_t value, nanosecond
object freq # frequency reference
list _date_attributes
def __hash__(_Timestamp self):
if self.nanosecond:
return hash(self.value)
return datetime.__hash__(self)
def __richcmp__(_Timestamp self, object other, int op):
cdef:
_Timestamp ots
int ndim
if isinstance(other, _Timestamp):
ots = other
elif other is NaT:
return op == Py_NE
elif PyDateTime_Check(other):
if self.nanosecond == 0:
val = self.to_pydatetime()
return PyObject_RichCompareBool(val, other, op)
try:
ots = Timestamp(other)
except ValueError:
return self._compare_outside_nanorange(other, op)
else:
ndim = getattr(other, "ndim", -1)
if ndim != -1:
if ndim == 0:
if is_datetime64_object(other):
other = Timestamp(other)
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
# only allow ==, != ops
raise TypeError('Cannot compare type %r with type %r' %
(type(self).__name__,
type(other).__name__))
elif is_array(other):
# avoid recursion error GH#15183
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
raise TypeError('Cannot compare type %r with type %r' %
(type(self).__name__, type(other).__name__))
self._assert_tzawareness_compat(other)
return cmp_scalar(self.value, ots.value, op)
def __reduce_ex__(self, protocol):
# python 3.6 compat
# http://bugs.python.org/issue28730
# now __reduce_ex__ is defined and higher priority than __reduce__
return self.__reduce__()
def __repr__(self):
stamp = self._repr_base
zone = None
try:
stamp += self.strftime('%z')
if self.tzinfo:
zone = get_timezone(self.tzinfo)
except ValueError:
year2000 = self.replace(year=2000)
stamp += year2000.strftime('%z')
if self.tzinfo:
zone = get_timezone(self.tzinfo)
try:
stamp += zone.strftime(' %%Z')
except:
pass
tz = ", tz='{0}'".format(zone) if zone is not None else ""
freq = "" if self.freq is None else ", freq='{0}'".format(self.freqstr)
return "Timestamp('{stamp}'{tz}{freq})".format(stamp=stamp,
tz=tz, freq=freq)
cdef bint _compare_outside_nanorange(_Timestamp self, datetime other,
int op) except -1:
cdef:
datetime dtval = self.to_pydatetime()
self._assert_tzawareness_compat(other)
if self.nanosecond == 0:
return PyObject_RichCompareBool(dtval, other, op)
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
elif op == Py_LT:
return dtval < other
elif op == Py_LE:
return dtval < other
elif op == Py_GT:
return dtval >= other
elif op == Py_GE:
return dtval >= other
cdef _assert_tzawareness_compat(_Timestamp self, datetime other):
if self.tzinfo is None:
if other.tzinfo is not None:
raise TypeError('Cannot compare tz-naive and tz-aware '
'timestamps')
elif other.tzinfo is None:
raise TypeError('Cannot compare tz-naive and tz-aware timestamps')
cpdef datetime to_pydatetime(_Timestamp self, bint warn=True):
"""
Convert a Timestamp object to a native Python datetime object.
If warn=True, issue a warning if nanoseconds is nonzero.
"""
if self.nanosecond != 0 and warn:
warnings.warn("Discarding nonzero nanoseconds in conversion",
UserWarning, stacklevel=2)
return datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo)
cpdef to_datetime64(self):
""" Returns a numpy.datetime64 object with 'ns' precision """
return np.datetime64(self.value, 'ns')
def __add__(self, other):
cdef:
int64_t other_int, nanos
if is_timedelta64_object(other):
other_int = other.astype('timedelta64[ns]').view('i8')
return Timestamp(self.value + other_int,
tz=self.tzinfo, freq=self.freq)
elif is_integer_object(other):
maybe_integer_op_deprecated(self)
if self is NaT:
# to be compat with Period
return NaT
elif self.freq is None:
raise ValueError("Cannot add integral value to Timestamp "
"without freq.")
return Timestamp((self.freq * other).apply(self), freq=self.freq)
elif PyDelta_Check(other) or hasattr(other, 'delta'):
# delta --> offsets.Tick
if self.tz is not None and getattr(other, '_prefix') == 'D':
warnings.warn("Day arithmetic will respect calendar day in a"
"future release", DeprecationWarning)
nanos = delta_to_nanoseconds(other)
result = Timestamp(self.value + nanos,
tz=self.tzinfo, freq=self.freq)
if getattr(other, 'normalize', False):
# DateOffset
result = result.normalize()
return result
# index/series like
elif hasattr(other, '_typ'):
return NotImplemented
result = datetime.__add__(self, other)
if PyDateTime_Check(result):
result = Timestamp(result)
result.nanosecond = self.nanosecond
return result
def __sub__(self, other):
if (is_timedelta64_object(other) or is_integer_object(other) or
PyDelta_Check(other) or hasattr(other, 'delta')):
# `delta` attribute is for offsets.Tick or offsets.Week obj
neg_other = -other
return self + neg_other
typ = getattr(other, '_typ', None)
# a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex
if typ in ('datetimeindex', 'datetimearray'):
# timezone comparison is performed in DatetimeIndex._sub_datelike
return -other.__sub__(self)
# a Timestamp-TimedeltaIndex -> yields a negative TimedeltaIndex
elif typ in ('timedeltaindex', 'timedeltaarray'):
return (-other).__add__(self)
elif other is NaT:
return NaT
# coerce if necessary if we are a Timestamp-like
if (PyDateTime_Check(self)
and (PyDateTime_Check(other) or is_datetime64_object(other))):
self = Timestamp(self)
other = Timestamp(other)
# validate tz's
if not tz_compare(self.tzinfo, other.tzinfo):
raise TypeError("Timestamp subtraction must have the "
"same timezones or no timezones")
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
# Timedelta
try:
return Timedelta(self.value - other.value)
except (OverflowError, OutOfBoundsDatetime):
pass
# scalar Timestamp/datetime - Timedelta -> yields a Timestamp (with
# same timezone if specified)
return datetime.__sub__(self, other)
cdef int64_t _maybe_convert_value_to_local(self):
"""Convert UTC i8 value to local i8 value if tz exists"""
cdef:
int64_t val
val = self.value
if self.tz is not None and not is_utc(self.tz):
val = tz_convert_single(self.value, UTC, self.tz)
return val
cpdef bint _get_start_end_field(self, str field):
cdef:
int64_t val
dict kwds
int8_t out[1]
int month_kw
freq = self.freq
if freq:
kwds = freq.kwds
month_kw = kwds.get('startingMonth', kwds.get('month', 12))
freqstr = self.freqstr
else:
month_kw = 12
freqstr = None
val = self._maybe_convert_value_to_local()
out = get_start_end_field(np.array([val], dtype=np.int64),
field, freqstr, month_kw)
return out[0]
cpdef _get_date_name_field(self, object field, object locale):
cdef:
int64_t val
object[:] out
val = self._maybe_convert_value_to_local()
out = get_date_name_field(np.array([val], dtype=np.int64),
field, locale=locale)
return out[0]
@property
def _repr_base(self):
return '{date} {time}'.format(date=self._date_repr,
time=self._time_repr)
@property
def _date_repr(self):
# Ideal here would be self.strftime("%Y-%m-%d"), but
# the datetime strftime() methods require year >= 1900
return '%d-%.2d-%.2d' % (self.year, self.month, self.day)
@property
def _time_repr(self):
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
if self.nanosecond != 0:
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
elif self.microsecond != 0:
result += '.%.6d' % self.microsecond
return result
@property
def _short_repr(self):
# format a Timestamp with only _date_repr if possible
# otherwise _repr_base
if (self.hour == 0 and
self.minute == 0 and
self.second == 0 and
self.microsecond == 0 and
self.nanosecond == 0):
return self._date_repr
return self._repr_base
@property
def asm8(self):
return np.datetime64(self.value, 'ns')
@property
def resolution(self):
"""
Return resolution describing the smallest difference between two
times that can be represented by Timestamp object_state
"""
# GH#21336, GH#21365
return Timedelta(nanoseconds=1)
def timestamp(self):
"""Return POSIX timestamp as float."""
# py27 compat, see GH#17329
return round(self.value / 1e9, 6)
# ----------------------------------------------------------------------
# Python front end to C extension type _Timestamp
# This serves as the box for datetime64
class Timestamp(_Timestamp):
"""Pandas replacement for datetime.datetime
Timestamp is the pandas equivalent of python's Datetime
and is interchangeable 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.
Parameters
----------
ts_input : datetime-like, str, int, float
Value to be converted to Timestamp
freq : str, DateOffset
Offset which Timestamp will have
tz : str, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
unit : str
Unit used for conversion if ts_input is of type int or float. The
valid values are 'D', 'h', 'm', 's', 'ms', 'us', and 'ns'. For
example, 's' means seconds and 'ms' means milliseconds.
year, month, day : int
.. versionadded:: 0.19.0
hour, minute, second, microsecond : int, optional, default 0
.. versionadded:: 0.19.0
nanosecond : int, optional, default 0
.. versionadded:: 0.23.0
tzinfo : datetime.tzinfo, optional, default None
.. versionadded:: 0.19.0
Notes
-----
There are essentially three calling conventions for the constructor. The
primary form accepts four parameters. They can be passed by position or
keyword.
The other two forms mimic the parameters from ``datetime.datetime``. They
can be passed by either position or keyword, but not both mixed together.
Examples
--------
Using the primary calling convention:
This converts a datetime-like string
>>> pd.Timestamp('2017-01-01T12')
Timestamp('2017-01-01 12:00:00')
This converts a float representing a Unix epoch in units of seconds
>>> pd.Timestamp(1513393355.5, unit='s')
Timestamp('2017-12-16 03:02:35.500000')
This converts an int representing a Unix-epoch in units of seconds
and for a particular timezone
>>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific')
Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')
Using the other two forms that mimic the API for ``datetime.datetime``:
>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')
>>> pd.Timestamp(year=2017, month=1, day=1, hour=12)
Timestamp('2017-01-01 12:00:00')
"""
@classmethod
def fromordinal(cls, ordinal, freq=None, tz=None):
"""
Timestamp.fromordinal(ordinal, freq=None, tz=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 : str, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
"""
return cls(datetime.fromordinal(ordinal),
freq=freq, tz=tz)
@classmethod
def now(cls, tz=None):
"""
Timestamp.now(tz=None)
Returns new Timestamp object representing current time local to
tz.
Parameters
----------
tz : str or timezone object, default None
Timezone to localize to
"""
if is_string_object(tz):
tz = maybe_get_tz(tz)
return cls(datetime.now(tz))
@classmethod
def today(cls, tz=None):
"""
Timestamp.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 : str or timezone object, default None
Timezone to localize to
"""
return cls.now(tz)
@classmethod
def utcnow(cls):
"""
Timestamp.utcnow()
Return a new Timestamp representing UTC day and time.
"""
return cls.now(UTC)
@classmethod
def utcfromtimestamp(cls, ts):
"""
Timestamp.utcfromtimestamp(ts)
Construct a naive UTC datetime from a POSIX timestamp.
"""
return cls(datetime.utcfromtimestamp(ts))
@classmethod
def fromtimestamp(cls, ts):
"""
Timestamp.fromtimestamp(ts)
timestamp[, tz] -> tz's local time from POSIX timestamp.
"""
return cls(datetime.fromtimestamp(ts))
@classmethod
def combine(cls, date, time):
"""
Timsetamp.combine(date, time)
date, time -> datetime with same date and time fields
"""
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,
nanosecond=None, tzinfo=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
_date_attributes = [year, month, day, hour, minute, second,
microsecond, nanosecond]
if tzinfo is not None:
if not PyTZInfo_Check(tzinfo):
# tzinfo must be a datetime.tzinfo object, GH#17690
raise TypeError('tzinfo must be a datetime.tzinfo object, '
'not %s' % type(tzinfo))
elif tz is not None:
raise ValueError('Can provide at most one of tz, tzinfo')
# User passed tzinfo instead of tz; avoid silently ignoring
tz, tzinfo = tzinfo, None
if is_string_object(ts_input):
# User passed a date string to parse.
# Check that the user didn't also pass a date attribute kwarg.
if any(arg is not None for arg in _date_attributes):
raise ValueError('Cannot pass a date attribute keyword '
'argument when passing a date string')
elif ts_input is _no_input:
# User passed keyword arguments.
ts_input = datetime(year, month, day, hour or 0,
minute or 0, second or 0,
microsecond or 0)
elif is_integer_object(freq):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
# microsecond[, nanosecond[, tzinfo]]]]]])
ts_input = datetime(ts_input, freq, tz, unit or 0,
year or 0, month or 0, day or 0)
nanosecond = hour
tz = minute
freq = None
if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
warnings.warn("Passing a datetime or Timestamp with tzinfo and the"
" tz parameter will raise in the future. Use"
" tz_convert instead.", FutureWarning)
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0)
if ts.value == NPY_NAT:
return NaT
if freq is None:
# GH 22311: Try to extract the frequency of a given Timestamp input
freq = getattr(ts_input, 'freq', None)
elif not is_offset_object(freq):
freq = to_offset(freq)
return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, freq)
def _round(self, freq, mode, ambiguous='raise', nonexistent='raise'):
if self.tz is not None:
value = self.tz_localize(None).value
else:
value = self.value
value = np.array([value], dtype=np.int64)
# Will only ever contain 1 element for timestamp
r = round_nsint64(value, mode, freq)[0]
result = Timestamp(r, unit='ns')
if self.tz is not None:
result = result.tz_localize(
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
)
return result
def round(self, freq, ambiguous='raise', nonexistent='raise'):
"""
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
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
.. versionadded:: 0.24.0
nonexistent : 'shift', 'NaT', default 'raise'
A nonexistent time does not exist in a particular timezone
where clocks moved forward due to DST.
- 'shift' will shift the nonexistent time forward to the closest
existing time
- 'NaT' will return NaT where there are nonexistent times
- 'raise' will raise an NonExistentTimeError if there are
nonexistent times
.. versionadded:: 0.24.0
Raises
------
ValueError if the freq cannot be converted
"""
return self._round(
freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent
)
def floor(self, freq, ambiguous='raise', nonexistent='raise'):
"""
return a new Timestamp floored to this resolution
Parameters
----------
freq : a freq string indicating the flooring resolution
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
.. versionadded:: 0.24.0
nonexistent : 'shift', 'NaT', default 'raise'
A nonexistent time does not exist in a particular timezone
where clocks moved forward due to DST.
- 'shift' will shift the nonexistent time forward to the closest
existing time
- 'NaT' will return NaT where there are nonexistent times
- 'raise' will raise an NonExistentTimeError if there are
nonexistent times
.. versionadded:: 0.24.0
Raises
------
ValueError if the freq cannot be converted
"""
return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent)
def ceil(self, freq, ambiguous='raise', nonexistent='raise'):
"""
return a new Timestamp ceiled to this resolution
Parameters
----------
freq : a freq string indicating the ceiling resolution
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
.. versionadded:: 0.24.0
nonexistent : 'shift', 'NaT', default 'raise'
A nonexistent time does not exist in a particular timezone
where clocks moved forward due to DST.
- 'shift' will shift the nonexistent time forward to the closest
existing time
- 'NaT' will return NaT where there are nonexistent times
- 'raise' will raise an NonExistentTimeError if there are
nonexistent times
.. versionadded:: 0.24.0
Raises
------
ValueError if the freq cannot be converted
"""
return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent)
@property
def tz(self):
"""
Alias for tzinfo
"""
return self.tzinfo
@tz.setter
def tz(self, value):
# GH 3746: Prevent localizing or converting the index by setting tz
raise AttributeError("Cannot directly set timezone. Use tz_localize() "
"or tz_convert() as appropriate")
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 self.tz is not None:
# GH#21333
warnings.warn("Converting to Period representation will "
"drop timezone information.",
UserWarning)
if freq is None:
freq = self.freq
return Period(self, freq=freq)
@property
def dayofweek(self):
return self.weekday()
def day_name(self, locale=None):
"""
Return the day name of the Timestamp with specified locale.
Parameters
----------
locale : string, default None (English locale)
locale determining the language in which to return the day name
Returns
-------
day_name : string
.. versionadded:: 0.23.0
"""
return self._get_date_name_field('day_name', locale)
def month_name(self, locale=None):
"""
Return the month name of the Timestamp with specified locale.
Parameters
----------
locale : string, default None (English locale)
locale determining the language in which to return the month name
Returns
-------
month_name : string
.. versionadded:: 0.23.0
"""
return self._get_date_name_field('month_name', locale)
@property
def weekday_name(self):
"""
.. deprecated:: 0.23.0
Use ``Timestamp.day_name()`` instead
"""
warnings.warn("`weekday_name` is deprecated and will be removed in a "
"future version. Use `day_name` instead",
FutureWarning)
return self.day_name()
@property
def dayofyear(self):
return ccalendar.get_day_of_year(self.year, self.month, self.day)
@property
def week(self):
return ccalendar.get_week_of_year(self.year, self.month, self.day)
weekofyear = week
@property
def quarter(self):
return ((self.month - 1) // 3) + 1
@property
def days_in_month(self):
return ccalendar.get_days_in_month(self.year, self.month)
daysinmonth = days_in_month
@property
def freqstr(self):
return getattr(self.freq, 'freqstr', self.freq)
@property
def is_month_start(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.day == 1
return self._get_start_end_field('is_month_start')
@property
def is_month_end(self):