-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy pathoffsets.pyx
5732 lines (4678 loc) · 177 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 re
import time
import warnings
from pandas.util._exceptions import find_stack_level
cimport cython
from cpython.datetime cimport (
PyDate_Check,
PyDateTime_Check,
PyDelta_Check,
date,
datetime,
import_datetime,
time as dt_time,
timedelta,
)
import warnings
import_datetime()
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 typing import ClassVar
from pandas._libs.properties import cache_readonly
from pandas._libs.tslibs cimport util
from pandas._libs.tslibs.util cimport (
is_float_object,
is_integer_object,
)
from pandas._libs.tslibs.ccalendar import (
MONTH_ALIASES,
int_to_weekday,
weekday_to_int,
)
from pandas.util._exceptions import find_stack_level
from pandas._libs.tslibs.ccalendar cimport (
MONTH_TO_CAL_NUM,
dayofweek,
get_days_in_month,
get_firstbday,
get_lastbday,
)
from pandas._libs.tslibs.conversion cimport localize_pydatetime
from pandas._libs.tslibs.dtypes cimport (
c_OFFSET_RENAMED_FREQSTR,
c_OFFSET_TO_PERIOD_FREQSTR,
c_PERIOD_AND_OFFSET_DEPR_FREQSTR,
c_PERIOD_TO_OFFSET_FREQSTR,
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,
import_pandas_datetime,
npy_datetimestruct,
npy_datetimestruct_to_datetime,
pandas_datetime_to_datetimestruct,
pydate_to_dtstruct,
)
import_pandas_datetime()
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 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_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 cnp.is_timedelta64_object(other)
):
# timedelta path
return func(self, other)
elif cnp.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)
result2 = Timestamp(result).as_unit(other.unit)
if result == result2:
# i.e. the conversion is non-lossy, not the case for e.g.
# test_milliseconds_combination
result = result2
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
res = result.tz_localize(None)
else:
res = result
value = res.as_unit("ns")._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 = tuple(sorted(_to_dt64D(dt) for dt in 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`
# The `naive` must be the `dt` naive wall time
# instead of the naive absolute time (GH#49441)
naive = dt.replace(tzinfo=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):
if not kwds:
# GH 45643/45890: (historically) defaults to 1 day
return timedelta(days=1), False
if "millisecond" in kwds:
raise NotImplementedError(
"Using DateOffset to replace `millisecond` component in "
"datetime object is not supported. Use "
"`microsecond=timestamp.microsecond % 1000 + ms * 1000` "
"instead."
)
nanos = {"nanosecond", "nanoseconds"}
# nanos are handled by apply_wraps
if all(k in nanos for k in kwds):
return timedelta(days=0), False
kwds_no_nanos = {k: v for k, v in kwds.items() if k not in nanos}
kwds_use_relativedelta = {
"year", "month", "day", "hour", "minute",
"second", "microsecond", "weekday", "years", "months", "weeks", "days",
"hours", "minutes", "seconds", "microseconds"
}
# "weeks" and "days" are left out despite being valid args for timedelta,
# because (historically) timedelta is used only for sub-daily.
kwds_use_timedelta = {
"seconds", "microseconds", "milliseconds", "minutes", "hours",
}
if all(k in kwds_use_timedelta for k in kwds_no_nanos):
# Sub-daily offset - use timedelta (tz-aware)
# This also handles "milliseconds" (plur): see GH 49897
return timedelta(**kwds_no_nanos), False
# convert milliseconds to microseconds, so relativedelta can parse it
if "milliseconds" in kwds_no_nanos:
micro = kwds_no_nanos.pop("milliseconds") * 1000
kwds_no_nanos["microseconds"] = kwds_no_nanos.get("microseconds", 0) + micro
if all(k in kwds_use_relativedelta for k in kwds_no_nanos):
from dateutil.relativedelta import relativedelta
return relativedelta(**kwds_no_nanos), True
raise ValueError(
f"Invalid argument/s or bad combination of arguments: {list(kwds.keys())}"
)
# ---------------------------------------------------------------------
# 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.
Parameters
----------
n : int
Number of multiples of the frequency.
normalize : bool
Whether the frequency can align with midnight.
Examples
--------
>>> pd.offsets.Hour(5).n
5
>>> pd.offsets.Hour(5).normalize
False
"""
# 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
# 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] != "_")}
params = tuple([str(type(self))] + sorted(attrs))
return params
@property
def kwds(self) -> dict:
"""
Return a dict of extra parameters for the offset.
See Also
--------
tseries.offsets.DateOffset : The base class for all pandas date offsets.
tseries.offsets.WeekOfMonth : Represents the week of the month.
tseries.offsets.LastWeekOfMonth : Represents the last week of the month.
Examples
--------
>>> pd.DateOffset(5).kwds
{}
>>> pd.offsets.FY5253Quarter().kwds
{'weekday': 0,
'startingMonth': 1,
'qtr_with_extra_week': 1,
'variation': 'nearest'}
"""
# 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 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) is type(self):
return type(self)(self.n - other.n, normalize=self.normalize,
**self.kwds)
else:
# e.g. PeriodIndex
return NotImplemented
def __rsub__(self, other):
return (-self).__add__(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 isinstance(other, BaseOffset):
# Otherwise raises RecurrsionError due to __rmul__
raise TypeError(
f"Cannot multiply {type(self).__name__} with "
f"{type(other).__name__}."
)
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 a copy of the frequency.
See Also
--------
tseries.offsets.Week.copy : Return a copy of Week offset.
tseries.offsets.DateOffset.copy : Return a copy of date offset.
tseries.offsets.MonthEnd.copy : Return a copy of MonthEnd offset.
tseries.offsets.YearBegin.copy : Return a copy of YearBegin offset.
Examples
--------
>>> freq = pd.DateOffset(1)
>>> freq_copy = freq.copy()
>>> freq is freq_copy
False
"""
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
def toDict(self) -> dict:
"""
Convert BaseOffset object to a dictionary representation
and used for JSON serialization.
"""
d = {}
# Add all attributes defined in _attributes
for attr in self._attributes:
if hasattr(self, attr):
d[attr] = getattr(self, attr)
return d
@property
def name(self) -> str:
"""
Return a string representing the base frequency.
See Also
--------
tseries.offsets.Week : Represents a weekly offset.
DateOffset : Base class for all other offset classes.
tseries.offsets.Day : Represents a single day offset.
tseries.offsets.MonthEnd : Represents a monthly offset that
snaps to the end of the month.
Examples
--------
>>> pd.offsets.Hour().name
'h'
>>> pd.offsets.Hour(5).name
'h'
"""
return self.rule_code
@property
def _prefix(self) -> str:
raise NotImplementedError("Prefix not defined")
@property
def rule_code(self) -> str:
"""
Return a string representing the base frequency.
See Also
--------
tseries.offsets.Hour.rule_code :
Returns a string representing the base frequency of 'h'.
tseries.offsets.Day.rule_code :
Returns a string representing the base frequency of 'D'.
Examples
--------
>>> pd.offsets.Hour().rule_code
'h'
>>> pd.offsets.Week(5).rule_code
'W'
"""
return self._prefix
@cache_readonly
def freqstr(self) -> str:
"""
Return a string representing the frequency.
See Also
--------
tseries.offsets.BusinessDay.freqstr :
Return a string representing an offset frequency in Business Days.
tseries.offsets.BusinessHour.freqstr :
Return a string representing an offset frequency in Business Hours.
tseries.offsets.Week.freqstr :
Return a string representing an offset frequency in Weeks.
tseries.offsets.Hour.freqstr :
Return a string representing an offset frequency in Hours.
Examples
--------
>>> pd.DateOffset(5).freqstr
'<5 * DateOffsets>'
>>> pd.offsets.BusinessHour(2).freqstr
'2bh'
>>> pd.offsets.Nano().freqstr
'ns'
>>> pd.offsets.Nano(-3).freqstr
'-3ns'
"""
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(self, other):
raise NotImplementedError("implemented by subclasses")
def _apply_array(self, dtarr: np.ndarray) -> np.ndarray:
# NB: _apply_array does not handle respecting `self.normalize`, the
# caller (DatetimeArray) handles that in post-processing.
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:
"""
Return boolean whether a timestamp intersects with this frequency.
This method determines if a given timestamp aligns with the start
of a custom business month, as defined by this offset. It accounts
for custom rules, such as skipping weekends or other non-business days,
and checks whether the provided datetime falls on a valid business day
that marks the beginning of the custom business month.
Parameters
----------
dt : datetime.datetime
Timestamp to check intersections with frequency.
See Also
--------
tseries.offsets.CustomBusinessMonthBegin : Represents the start of a custom
business month.
tseries.offsets.CustomBusinessMonthEnd : Represents the end of a custom
business month.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Day(1)
>>> freq.is_on_offset(ts)
True
>>> ts = pd.Timestamp(2022, 8, 6)
>>> ts.day_name()
'Saturday'
>>> freq = pd.offsets.BusinessDay(1)
>>> freq.is_on_offset(ts)
False
"""
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 cnp.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 picklable 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):
"""
Returns a integer of the total number of nanoseconds for fixed frequencies.
Raises
------
ValueError
If the frequency is non-fixed.
See Also
--------
tseries.offsets.Hour.nanos :
Returns an integer of the total number of nanoseconds.
tseries.offsets.Day.nanos :
Returns an integer of the total number of nanoseconds.
Examples
--------
>>> pd.offsets.Week(n=1).nanos
ValueError: Week: weekday=None is a non-fixed frequency
"""
raise ValueError(f"{self} is a non-fixed frequency")
# ------------------------------------------------------------------
def is_month_start(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the month start.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_month_end : Return boolean whether a timestamp occurs on the month end.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_month_start(ts)
True
"""
return ts._get_start_end_field("is_month_start", self)
def is_month_end(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the month end.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_month_start : Return boolean whether a timestamp occurs on the month start.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_month_end(ts)
False
"""
return ts._get_start_end_field("is_month_end", self)
def is_quarter_start(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the quarter start.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_quarter_end : Return boolean whether a timestamp occurs on the quarter end.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_quarter_start(ts)
True
"""
return ts._get_start_end_field("is_quarter_start", self)
def is_quarter_end(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the quarter end.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_quarter_start : Return boolean whether a timestamp
occurs on the quarter start.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_quarter_end(ts)
False
"""
return ts._get_start_end_field("is_quarter_end", self)
def is_year_start(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the year start.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_year_end : Return boolean whether a timestamp occurs on the year end.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_year_start(ts)
True
"""
return ts._get_start_end_field("is_year_start", self)
def is_year_end(self, _Timestamp ts):
"""
Return boolean whether a timestamp occurs on the year end.
Parameters
----------
ts : Timestamp
The timestamp to check.
See Also
--------
is_year_start : Return boolean whether a timestamp occurs on the year start.
Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> freq = pd.offsets.Hour(5)
>>> freq.is_year_end(ts)
False
"""
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