forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoffsets.pyx
4158 lines (3404 loc) · 128 KB
/
offsets.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
import operator
import re
import time
import warnings
cimport cython
from cpython.datetime cimport (
PyDate_Check,
PyDateTime_Check,
PyDelta_Check,
date,
datetime,
import_datetime,
time as dt_time,
timedelta,
)
import_datetime()
from dateutil.easter import easter
from dateutil.relativedelta import relativedelta
import numpy as np
cimport numpy as cnp
from numpy cimport (
int64_t,
ndarray,
)
cnp.import_array()
# TODO: formalize having _libs.properties "above" tslibs in the dependency structure
from pandas._libs.properties import cache_readonly
from pandas._libs.tslibs cimport util
from pandas._libs.tslibs.util cimport (
is_datetime64_object,
is_float_object,
is_integer_object,
)
from pandas._libs.tslibs.ccalendar import (
MONTH_ALIASES,
MONTH_TO_CAL_NUM,
int_to_weekday,
weekday_to_int,
)
from pandas._libs.tslibs.ccalendar cimport (
dayofweek,
get_days_in_month,
get_firstbday,
get_lastbday,
)
from pandas._libs.tslibs.conversion cimport localize_pydatetime
from pandas._libs.tslibs.dtypes cimport periods_per_day
from pandas._libs.tslibs.nattype cimport (
NPY_NAT,
c_NaT as NaT,
)
from pandas._libs.tslibs.np_datetime cimport (
NPY_DATETIMEUNIT,
get_unit_from_dtype,
npy_datetimestruct,
npy_datetimestruct_to_datetime,
pandas_datetime_to_datetimestruct,
pydate_to_dtstruct,
)
from .dtypes cimport PeriodDtypeCode
from .timedeltas cimport (
_Timedelta,
delta_to_nanoseconds,
is_any_td_scalar,
)
from .timedeltas import Timedelta
from .timestamps cimport _Timestamp
from .timestamps import Timestamp
# ---------------------------------------------------------------------
# Misc Helpers
cdef bint is_offset_object(object obj):
return isinstance(obj, BaseOffset)
cdef bint is_tick_object(object obj):
return isinstance(obj, Tick)
cdef datetime _as_datetime(datetime obj):
if isinstance(obj, _Timestamp):
return obj.to_pydatetime()
return obj
cdef bint _is_normalized(datetime dt):
if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
# Regardless of whether dt is datetime vs Timestamp
return False
if isinstance(dt, _Timestamp):
return dt.nanosecond == 0
return True
def apply_wrapper_core(func, self, other) -> ndarray:
result = func(self, other)
result = np.asarray(result)
if self.normalize:
# TODO: Avoid circular/runtime import
from .vectorized import normalize_i8_timestamps
reso = get_unit_from_dtype(other.dtype)
result = normalize_i8_timestamps(result.view("i8"), None, reso=reso)
return result
def apply_array_wraps(func):
# Note: normally we would use `@functools.wraps(func)`, but this does
# not play nicely with cython class methods
def wrapper(self, other) -> np.ndarray:
# other is a DatetimeArray
result = apply_wrapper_core(func, self, other)
return result
# do @functools.wraps(func) manually since it doesn't work on cdef funcs
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
def apply_wraps(func):
# Note: normally we would use `@functools.wraps(func)`, but this does
# not play nicely with cython class methods
def wrapper(self, other):
if other is NaT:
return NaT
elif (
isinstance(other, BaseOffset)
or PyDelta_Check(other)
or util.is_timedelta64_object(other)
):
# timedelta path
return func(self, other)
elif is_datetime64_object(other) or PyDate_Check(other):
# PyDate_Check includes date, datetime
other = Timestamp(other)
else:
# This will end up returning NotImplemented back in __add__
raise ApplyTypeError
tz = other.tzinfo
nano = other.nanosecond
if self._adjust_dst:
other = other.tz_localize(None)
result = func(self, other)
result = Timestamp(result)
if self._adjust_dst:
result = result.tz_localize(tz)
if self.normalize:
result = result.normalize()
# If the offset object does not have a nanoseconds component,
# the result's nanosecond component may be lost.
if not self.normalize and nano != 0 and not hasattr(self, "nanoseconds"):
if result.nanosecond != nano:
if result.tz is not None:
# convert to UTC
value = result.tz_localize(None).value
else:
value = result.value
result = Timestamp(value + nano)
if tz is not None and result.tzinfo is None:
result = result.tz_localize(tz)
return result
# do @functools.wraps(func) manually since it doesn't work on cdef funcs
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
cdef _wrap_timedelta_result(result):
"""
Tick operations dispatch to their Timedelta counterparts. Wrap the result
of these operations in a Tick if possible.
Parameters
----------
result : object
Returns
-------
object
"""
if PyDelta_Check(result):
# convert Timedelta back to a Tick
return delta_to_tick(result)
return result
# ---------------------------------------------------------------------
# Business Helpers
cdef _get_calendar(weekmask, holidays, calendar):
"""
Generate busdaycalendar
"""
if isinstance(calendar, np.busdaycalendar):
if not holidays:
holidays = tuple(calendar.holidays)
elif not isinstance(holidays, tuple):
holidays = tuple(holidays)
else:
# trust that calendar.holidays and holidays are
# consistent
pass
return calendar, holidays
if holidays is None:
holidays = []
try:
holidays = holidays + calendar.holidays().tolist()
except AttributeError:
pass
holidays = [_to_dt64D(dt) for dt in holidays]
holidays = tuple(sorted(holidays))
kwargs = {'weekmask': weekmask}
if holidays:
kwargs['holidays'] = holidays
busdaycalendar = np.busdaycalendar(**kwargs)
return busdaycalendar, holidays
cdef _to_dt64D(dt):
# Currently
# > np.datetime64(dt.datetime(2013,5,1),dtype='datetime64[D]')
# numpy.datetime64('2013-05-01T02:00:00.000000+0200')
# Thus astype is needed to cast datetime to datetime64[D]
if getattr(dt, 'tzinfo', None) is not None:
# Get the nanosecond timestamp,
# equiv `Timestamp(dt).value` or `dt.timestamp() * 10**9`
naive = dt.astimezone(None)
dt = np.datetime64(naive, "D")
else:
dt = np.datetime64(dt)
if dt.dtype.name != "datetime64[D]":
dt = dt.astype("datetime64[D]")
return dt
# ---------------------------------------------------------------------
# Validation
cdef _validate_business_time(t_input):
if isinstance(t_input, str):
try:
t = time.strptime(t_input, '%H:%M')
return dt_time(hour=t.tm_hour, minute=t.tm_min)
except ValueError:
raise ValueError("time data must match '%H:%M' format")
elif isinstance(t_input, dt_time):
if t_input.second != 0 or t_input.microsecond != 0:
raise ValueError(
"time data must be specified only with hour and minute")
return t_input
else:
raise ValueError("time data must be string or datetime.time")
# ---------------------------------------------------------------------
# Constructor Helpers
_relativedelta_kwds = {"years", "months", "weeks", "days", "year", "month",
"day", "weekday", "hour", "minute", "second",
"microsecond", "millisecond", "nanosecond",
"nanoseconds", "hours", "minutes", "seconds",
"milliseconds", "microseconds"}
cdef _determine_offset(kwds):
# timedelta is used for sub-daily plural offsets and all singular
# offsets relativedelta is used for plural offsets of daily length or
# more nanosecond(s) are handled by apply_wraps
kwds_no_nanos = dict(
(k, v) for k, v in kwds.items()
if k not in ('nanosecond', 'nanoseconds')
)
# TODO: Are nanosecond and nanoseconds allowed somewhere?
_kwds_use_relativedelta = ('years', 'months', 'weeks', 'days',
'year', 'month', 'week', 'day', 'weekday',
'hour', 'minute', 'second', 'microsecond',
'millisecond')
use_relativedelta = False
if len(kwds_no_nanos) > 0:
if any(k in _kwds_use_relativedelta for k in kwds_no_nanos):
if "millisecond" in kwds_no_nanos:
raise NotImplementedError(
"Using DateOffset to replace `millisecond` component in "
"datetime object is not supported. Use "
"`microsecond=timestamp.microsecond % 1000 + ms * 1000` "
"instead."
)
offset = relativedelta(**kwds_no_nanos)
use_relativedelta = True
else:
# sub-daily offset - use timedelta (tz-aware)
offset = timedelta(**kwds_no_nanos)
elif any(nano in kwds for nano in ('nanosecond', 'nanoseconds')):
offset = timedelta(days=0)
else:
# GH 45643/45890: (historically) defaults to 1 day for non-nano
# since datetime.timedelta doesn't handle nanoseconds
offset = timedelta(days=1)
return offset, use_relativedelta
# ---------------------------------------------------------------------
# Mixins & Singletons
class ApplyTypeError(TypeError):
# sentinel class for catching the apply error to return NotImplemented
pass
# ---------------------------------------------------------------------
# Base Classes
cdef class BaseOffset:
"""
Base class for DateOffset methods that are not overridden by subclasses.
"""
# ensure that reversed-ops with numpy scalars return NotImplemented
__array_priority__ = 1000
_day_opt = None
_attributes = tuple(["n", "normalize"])
_use_relativedelta = False
_adjust_dst = True
_deprecations = frozenset(["isAnchored", "onOffset"])
# cdef readonly:
# int64_t n
# bint normalize
# dict _cache
def __init__(self, n=1, normalize=False):
n = self._validate_n(n)
self.n = n
self.normalize = normalize
self._cache = {}
def __eq__(self, other) -> bool:
if isinstance(other, str):
try:
# GH#23524 if to_offset fails, we are dealing with an
# incomparable type so == is False and != is True
other = to_offset(other)
except ValueError:
# e.g. "infer"
return False
try:
return self._params == other._params
except AttributeError:
# other is not a DateOffset object
return False
def __ne__(self, other):
return not self == other
def __hash__(self) -> int:
return hash(self._params)
@cache_readonly
def _params(self):
"""
Returns a tuple containing all of the attributes needed to evaluate
equality between two DateOffset objects.
"""
d = getattr(self, "__dict__", {})
all_paras = d.copy()
all_paras["n"] = self.n
all_paras["normalize"] = self.normalize
for attr in self._attributes:
if hasattr(self, attr) and attr not in d:
# cython attributes are not in __dict__
all_paras[attr] = getattr(self, attr)
if 'holidays' in all_paras and not all_paras['holidays']:
all_paras.pop('holidays')
exclude = ['kwds', 'name', 'calendar']
attrs = [(k, v) for k, v in all_paras.items()
if (k not in exclude) and (k[0] != '_')]
attrs = sorted(set(attrs))
params = tuple([str(type(self))] + attrs)
return params
@property
def kwds(self) -> dict:
# for backwards-compatibility
kwds = {name: getattr(self, name, None) for name in self._attributes
if name not in ["n", "normalize"]}
return {name: kwds[name] for name in kwds if kwds[name] is not None}
@property
def base(self):
"""
Returns a copy of the calling offset object with n=1 and all other
attributes equal.
"""
return type(self)(n=1, normalize=self.normalize, **self.kwds)
def __add__(self, other):
if not isinstance(self, BaseOffset):
# cython semantics; this is __radd__
# TODO(cython3): remove this, this moved to __radd__
return other.__add__(self)
elif util.is_array(other) and other.dtype == object:
return np.array([self + x for x in other])
try:
return self._apply(other)
except ApplyTypeError:
return NotImplemented
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
if PyDateTime_Check(other):
raise TypeError('Cannot subtract datetime from offset.')
elif type(other) == type(self):
return type(self)(self.n - other.n, normalize=self.normalize,
**self.kwds)
elif not isinstance(self, BaseOffset):
# TODO(cython3): remove, this moved to __rsub__
# cython semantics, this is __rsub__
return (-other).__add__(self)
else:
# e.g. PeriodIndex
return NotImplemented
def __rsub__(self, other):
return (-self).__add__(other)
def __call__(self, other):
warnings.warn(
"DateOffset.__call__ is deprecated and will be removed in a future "
"version. Use `offset + other` instead.",
FutureWarning,
stacklevel=1,
)
return self._apply(other)
def apply(self, other):
# GH#44522
warnings.warn(
f"{type(self).__name__}.apply is deprecated and will be removed "
"in a future version. Use `offset + other` instead",
FutureWarning,
stacklevel=2,
)
return self._apply(other)
def __mul__(self, other):
if util.is_array(other):
return np.array([self * x for x in other])
elif is_integer_object(other):
return type(self)(n=other * self.n, normalize=self.normalize,
**self.kwds)
elif not isinstance(self, BaseOffset):
# TODO(cython3): remove this, this moved to __rmul__
# cython semantics, this is __rmul__
return other.__mul__(self)
return NotImplemented
def __rmul__(self, other):
return self.__mul__(other)
def __neg__(self):
# Note: we are deferring directly to __mul__ instead of __rmul__, as
# that allows us to use methods that can go in a `cdef class`
return self * -1
def copy(self):
# Note: we are deferring directly to __mul__ instead of __rmul__, as
# that allows us to use methods that can go in a `cdef class`
return self * 1
# ------------------------------------------------------------------
# Name and Rendering Methods
def __repr__(self) -> str:
# _output_name used by B(Year|Quarter)(End|Begin) to
# expand "B" -> "Business"
class_name = getattr(self, "_output_name", type(self).__name__)
if abs(self.n) != 1:
plural = "s"
else:
plural = ""
n_str = ""
if self.n != 1:
n_str = f"{self.n} * "
out = f"<{n_str}{class_name}{plural}{self._repr_attrs()}>"
return out
def _repr_attrs(self) -> str:
exclude = {"n", "inc", "normalize"}
attrs = []
for attr in sorted(self._attributes):
# _attributes instead of __dict__ because cython attrs are not in __dict__
if attr.startswith("_") or attr == "kwds" or not hasattr(self, attr):
# DateOffset may not have some of these attributes
continue
elif attr not in exclude:
value = getattr(self, attr)
attrs.append(f"{attr}={value}")
out = ""
if attrs:
out += ": " + ", ".join(attrs)
return out
@property
def name(self) -> str:
return self.rule_code
@property
def _prefix(self) -> str:
raise NotImplementedError("Prefix not defined")
@property
def rule_code(self) -> str:
return self._prefix
@cache_readonly
def freqstr(self) -> str:
try:
code = self.rule_code
except NotImplementedError:
return str(repr(self))
if self.n != 1:
fstr = f"{self.n}{code}"
else:
fstr = code
try:
if self._offset:
fstr += self._offset_str()
except AttributeError:
# TODO: standardize `_offset` vs `offset` naming convention
pass
return fstr
def _offset_str(self) -> str:
return ""
# ------------------------------------------------------------------
def apply_index(self, dtindex):
"""
Vectorized apply of DateOffset to DatetimeIndex,
raises NotImplementedError for offsets without a
vectorized implementation.
.. deprecated:: 1.1.0
Use ``offset + dtindex`` instead.
Parameters
----------
index : DatetimeIndex
Returns
-------
DatetimeIndex
Raises
------
NotImplementedError
When the specific offset subclass does not have a vectorized
implementation.
"""
warnings.warn("'Offset.apply_index(other)' is deprecated. "
"Use 'offset + other' instead.", FutureWarning)
res = self._apply_array(dtindex)
return type(dtindex)(res)
@apply_array_wraps
def _apply_array(self, dtarr):
raise NotImplementedError(
f"DateOffset subclass {type(self).__name__} "
"does not have a vectorized implementation"
)
def rollback(self, dt) -> datetime:
"""
Roll provided date backward to next offset only if not on offset.
Returns
-------
TimeStamp
Rolled timestamp if not on offset, otherwise unchanged timestamp.
"""
dt = Timestamp(dt)
if not self.is_on_offset(dt):
dt = dt - type(self)(1, normalize=self.normalize, **self.kwds)
return dt
def rollforward(self, dt) -> datetime:
"""
Roll provided date forward to next offset only if not on offset.
Returns
-------
TimeStamp
Rolled timestamp if not on offset, otherwise unchanged timestamp.
"""
dt = Timestamp(dt)
if not self.is_on_offset(dt):
dt = dt + type(self)(1, normalize=self.normalize, **self.kwds)
return dt
def _get_offset_day(self, other: datetime) -> int:
# subclass must implement `_day_opt`; calling from the base class
# will implicitly assume day_opt = "business_end", see get_day_of_month.
cdef:
npy_datetimestruct dts
pydate_to_dtstruct(other, &dts)
return get_day_of_month(&dts, self._day_opt)
def is_on_offset(self, dt: datetime) -> bool:
if self.normalize and not _is_normalized(dt):
return False
# Default (slow) method for determining if some date is a member of the
# date range generated by this offset. Subclasses may have this
# re-implemented in a nicer way.
a = dt
b = (dt + self) - self
return a == b
# ------------------------------------------------------------------
# Staticmethod so we can call from Tick.__init__, will be unnecessary
# once BaseOffset is a cdef class and is inherited by Tick
@staticmethod
def _validate_n(n) -> int:
"""
Require that `n` be an integer.
Parameters
----------
n : int
Returns
-------
nint : int
Raises
------
TypeError if `int(n)` raises
ValueError if n != int(n)
"""
if util.is_timedelta64_object(n):
raise TypeError(f'`n` argument must be an integer, got {type(n)}')
try:
nint = int(n)
except (ValueError, TypeError):
raise TypeError(f'`n` argument must be an integer, got {type(n)}')
if n != nint:
raise ValueError(f'`n` argument must be an integer, got {n}')
return nint
def __setstate__(self, state):
"""
Reconstruct an instance from a pickled state
"""
self.n = state.pop("n")
self.normalize = state.pop("normalize")
self._cache = state.pop("_cache", {})
# At this point we expect state to be empty
def __getstate__(self):
"""
Return a pickleable state
"""
state = {}
state["n"] = self.n
state["normalize"] = self.normalize
# we don't want to actually pickle the calendar object
# as its a np.busyday; we recreate on deserialization
state.pop("calendar", None)
if "kwds" in state:
state["kwds"].pop("calendar", None)
return state
@property
def nanos(self):
raise ValueError(f"{self} is a non-fixed frequency")
def onOffset(self, dt) -> bool:
warnings.warn(
"onOffset is a deprecated, use is_on_offset instead.",
FutureWarning,
stacklevel=1,
)
return self.is_on_offset(dt)
def isAnchored(self) -> bool:
warnings.warn(
"isAnchored is a deprecated, use is_anchored instead.",
FutureWarning,
stacklevel=1,
)
return self.is_anchored()
def is_anchored(self) -> bool:
# TODO: Does this make sense for the general case? It would help
# if there were a canonical docstring for what is_anchored means.
return self.n == 1
# ------------------------------------------------------------------
def is_month_start(self, _Timestamp ts):
return ts._get_start_end_field("is_month_start", self)
def is_month_end(self, _Timestamp ts):
return ts._get_start_end_field("is_month_end", self)
def is_quarter_start(self, _Timestamp ts):
return ts._get_start_end_field("is_quarter_start", self)
def is_quarter_end(self, _Timestamp ts):
return ts._get_start_end_field("is_quarter_end", self)
def is_year_start(self, _Timestamp ts):
return ts._get_start_end_field("is_year_start", self)
def is_year_end(self, _Timestamp ts):
return ts._get_start_end_field("is_year_end", self)
cdef class SingleConstructorOffset(BaseOffset):
@classmethod
def _from_name(cls, suffix=None):
# default _from_name calls cls with no args
if suffix:
raise ValueError(f"Bad freq suffix {suffix}")
return cls()
def __reduce__(self):
# This __reduce__ implementation is for all BaseOffset subclasses
# except for RelativeDeltaOffset
# np.busdaycalendar objects do not pickle nicely, but we can reconstruct
# from attributes that do get pickled.
tup = tuple(
getattr(self, attr) if attr != "calendar" else None
for attr in self._attributes
)
return type(self), tup
# ---------------------------------------------------------------------
# Tick Offsets
cdef class Tick(SingleConstructorOffset):
_adjust_dst = False
_prefix = "undefined"
_attributes = tuple(["n", "normalize"])
def __init__(self, n=1, normalize=False):
n = self._validate_n(n)
self.n = n
self.normalize = False
self._cache = {}
if normalize:
# GH#21427
raise ValueError(
"Tick offset with `normalize=True` are not allowed."
)
# Note: Without making this cpdef, we get AttributeError when calling
# from __mul__
cpdef Tick _next_higher_resolution(Tick self):
if type(self) is Day:
return Hour(self.n * 24)
if type(self) is Hour:
return Minute(self.n * 60)
if type(self) is Minute:
return Second(self.n * 60)
if type(self) is Second:
return Milli(self.n * 1000)
if type(self) is Milli:
return Micro(self.n * 1000)
if type(self) is Micro:
return Nano(self.n * 1000)
raise ValueError("Could not convert to integer offset at any resolution")
# --------------------------------------------------------------------
def _repr_attrs(self) -> str:
# Since cdef classes have no __dict__, we need to override
return ""
@property
def delta(self):
return self.n * Timedelta(self._nanos_inc)
@property
def nanos(self) -> int64_t:
return self.n * self._nanos_inc
def is_on_offset(self, dt: datetime) -> bool:
return True
def is_anchored(self) -> bool:
return False
# This is identical to BaseOffset.__hash__, but has to be redefined here
# for Python 3, because we've redefined __eq__.
def __hash__(self) -> int:
return hash(self._params)
# --------------------------------------------------------------------
# Comparison and Arithmetic Methods
def __eq__(self, other):
if isinstance(other, str):
try:
# GH#23524 if to_offset fails, we are dealing with an
# incomparable type so == is False and != is True
other = to_offset(other)
except ValueError:
# e.g. "infer"
return False
return self.delta == other
def __ne__(self, other):
return not (self == other)
def __le__(self, other):
return self.delta.__le__(other)
def __lt__(self, other):
return self.delta.__lt__(other)
def __ge__(self, other):
return self.delta.__ge__(other)
def __gt__(self, other):
return self.delta.__gt__(other)
def __mul__(self, other):
if not isinstance(self, Tick):
# TODO(cython3), remove this, this moved to __rmul__
# cython semantics, this is __rmul__
return other.__mul__(self)
if is_float_object(other):
n = other * self.n
# If the new `n` is an integer, we can represent it using the
# same Tick subclass as self, otherwise we need to move up
# to a higher-resolution subclass
if np.isclose(n % 1, 0):
return type(self)(int(n))
new_self = self._next_higher_resolution()
return new_self * other
return BaseOffset.__mul__(self, other)
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
if not isinstance(self, Tick):
# cython semantics mean the args are sometimes swapped
result = other.delta.__rtruediv__(self)
else:
result = self.delta.__truediv__(other)
return _wrap_timedelta_result(result)
def __rtruediv__(self, other):
result = self.delta.__rtruediv__(other)
return _wrap_timedelta_result(result)
def __add__(self, other):
if not isinstance(self, Tick):
# cython semantics; this is __radd__
# TODO(cython3): remove this, this moved to __radd__
return other.__add__(self)
if isinstance(other, Tick):
if type(self) == type(other):
return type(self)(self.n + other.n)
else:
return delta_to_tick(self.delta + other.delta)
try:
return self._apply(other)
except ApplyTypeError:
# Includes pd.Period
return NotImplemented
except OverflowError as err:
raise OverflowError(
f"the add operation between {self} and {other} will overflow"
) from err
def __radd__(self, other):
return self.__add__(other)
def _apply(self, other):
# Timestamp can handle tz and nano sec, thus no need to use apply_wraps
if isinstance(other, _Timestamp):
# GH#15126
return other + self.delta
elif other is NaT:
return NaT
elif is_datetime64_object(other) or PyDate_Check(other):
# PyDate_Check includes date, datetime
return Timestamp(other) + self
if util.is_timedelta64_object(other) or PyDelta_Check(other):
return other + self.delta
elif isinstance(other, type(self)):
# TODO(2.0): remove once apply deprecation is enforced.
# This is reached in tests that specifically call apply,
# but should not be reached "naturally" because __add__ should
# catch this case first.
return type(self)(self.n + other.n)
raise ApplyTypeError(f"Unhandled type: {type(other).__name__}")
# --------------------------------------------------------------------
# Pickle Methods
def __setstate__(self, state):
self.n = state["n"]
self.normalize = False
cdef class Day(Tick):
_nanos_inc = 24 * 3600 * 1_000_000_000
_prefix = "D"
_period_dtype_code = PeriodDtypeCode.D
_reso = NPY_DATETIMEUNIT.NPY_FR_D
cdef class Hour(Tick):
_nanos_inc = 3600 * 1_000_000_000
_prefix = "H"
_period_dtype_code = PeriodDtypeCode.H
_reso = NPY_DATETIMEUNIT.NPY_FR_h
cdef class Minute(Tick):
_nanos_inc = 60 * 1_000_000_000
_prefix = "T"
_period_dtype_code = PeriodDtypeCode.T
_reso = NPY_DATETIMEUNIT.NPY_FR_m
cdef class Second(Tick):
_nanos_inc = 1_000_000_000
_prefix = "S"
_period_dtype_code = PeriodDtypeCode.S
_reso = NPY_DATETIMEUNIT.NPY_FR_s
cdef class Milli(Tick):
_nanos_inc = 1_000_000
_prefix = "L"
_period_dtype_code = PeriodDtypeCode.L
_reso = NPY_DATETIMEUNIT.NPY_FR_ms