13
13
14
14
from pandas import _np_version_under1p7
15
15
16
+ import functools
17
+
16
18
__all__ = ['Day' , 'BusinessDay' , 'BDay' , 'CustomBusinessDay' , 'CDay' ,
17
19
'MonthBegin' , 'BMonthBegin' , 'MonthEnd' , 'BMonthEnd' ,
18
20
'YearBegin' , 'BYearBegin' , 'YearEnd' , 'BYearEnd' ,
@@ -35,6 +37,15 @@ def as_datetime(obj):
35
37
obj = f ()
36
38
return obj
37
39
40
+ def apply_nat (func ):
41
+ @functools .wraps (func )
42
+ def wrapper (self , other ):
43
+ if other is tslib .NaT :
44
+ return tslib .NaT
45
+ else :
46
+ return func (self , other )
47
+ return wrapper
48
+
38
49
#----------------------------------------------------------------------
39
50
# DateOffset
40
51
@@ -102,6 +113,7 @@ def __init__(self, n=1, **kwds):
102
113
else :
103
114
self ._offset = timedelta (1 )
104
115
116
+ @apply_nat
105
117
def apply (self , other ):
106
118
other = as_datetime (other )
107
119
if len (self .kwds ) > 0 :
@@ -382,6 +394,7 @@ def get_str(td):
382
394
def isAnchored (self ):
383
395
return (self .n == 1 )
384
396
397
+ @apply_nat
385
398
def apply (self , other ):
386
399
if isinstance (other , datetime ):
387
400
n = self .n
@@ -502,6 +515,7 @@ def __setstate__(self, state):
502
515
self .__dict__ = state
503
516
self ._set_busdaycalendar ()
504
517
518
+ @apply_nat
505
519
def apply (self , other ):
506
520
if self .n <= 0 :
507
521
roll = 'forward'
@@ -582,6 +596,7 @@ def name(self):
582
596
class MonthEnd (MonthOffset ):
583
597
"""DateOffset of one month end"""
584
598
599
+ @apply_nat
585
600
def apply (self , other ):
586
601
other = datetime (other .year , other .month , other .day ,
587
602
tzinfo = other .tzinfo )
@@ -606,6 +621,7 @@ def onOffset(cls, dt):
606
621
class MonthBegin (MonthOffset ):
607
622
"""DateOffset of one month at beginning"""
608
623
624
+ @apply_nat
609
625
def apply (self , other ):
610
626
n = self .n
611
627
@@ -628,6 +644,7 @@ class BusinessMonthEnd(MonthOffset):
628
644
def isAnchored (self ):
629
645
return (self .n == 1 )
630
646
647
+ @apply_nat
631
648
def apply (self , other ):
632
649
other = datetime (other .year , other .month , other .day )
633
650
@@ -653,6 +670,7 @@ def apply(self, other):
653
670
class BusinessMonthBegin (MonthOffset ):
654
671
"""DateOffset of one business month at beginning"""
655
672
673
+ @apply_nat
656
674
def apply (self , other ):
657
675
n = self .n
658
676
@@ -710,6 +728,7 @@ def __init__(self, n=1, **kwds):
710
728
def isAnchored (self ):
711
729
return (self .n == 1 and self .weekday is not None )
712
730
731
+ @apply_nat
713
732
def apply (self , other ):
714
733
if self .weekday is None :
715
734
return as_timestamp (as_datetime (other ) + self .n * self ._inc )
@@ -811,6 +830,7 @@ def __init__(self, n=1, **kwds):
811
830
812
831
self .kwds = kwds
813
832
833
+ @apply_nat
814
834
def apply (self , other ):
815
835
offsetOfMonth = self .getOffsetOfMonth (other )
816
836
@@ -890,6 +910,7 @@ def __init__(self, n=1, **kwds):
890
910
891
911
self .kwds = kwds
892
912
913
+ @apply_nat
893
914
def apply (self , other ):
894
915
offsetOfMonth = self .getOffsetOfMonth (other )
895
916
@@ -983,6 +1004,7 @@ class BQuarterEnd(QuarterOffset):
983
1004
_from_name_startingMonth = 12
984
1005
_prefix = 'BQ'
985
1006
1007
+ @apply_nat
986
1008
def apply (self , other ):
987
1009
n = self .n
988
1010
@@ -1037,6 +1059,7 @@ class BQuarterBegin(QuarterOffset):
1037
1059
_from_name_startingMonth = 1
1038
1060
_prefix = 'BQS'
1039
1061
1062
+ @apply_nat
1040
1063
def apply (self , other ):
1041
1064
n = self .n
1042
1065
other = as_datetime (other )
@@ -1086,6 +1109,7 @@ def __init__(self, n=1, **kwds):
1086
1109
def isAnchored (self ):
1087
1110
return (self .n == 1 and self .startingMonth is not None )
1088
1111
1112
+ @apply_nat
1089
1113
def apply (self , other ):
1090
1114
n = self .n
1091
1115
other = as_datetime (other )
@@ -1117,6 +1141,7 @@ class QuarterBegin(QuarterOffset):
1117
1141
def isAnchored (self ):
1118
1142
return (self .n == 1 and self .startingMonth is not None )
1119
1143
1144
+ @apply_nat
1120
1145
def apply (self , other ):
1121
1146
n = self .n
1122
1147
other = as_datetime (other )
@@ -1166,6 +1191,7 @@ class BYearEnd(YearOffset):
1166
1191
_default_month = 12
1167
1192
_prefix = 'BA'
1168
1193
1194
+ @apply_nat
1169
1195
def apply (self , other ):
1170
1196
n = self .n
1171
1197
other = as_datetime (other )
@@ -1203,6 +1229,7 @@ class BYearBegin(YearOffset):
1203
1229
_default_month = 1
1204
1230
_prefix = 'BAS'
1205
1231
1232
+ @apply_nat
1206
1233
def apply (self , other ):
1207
1234
n = self .n
1208
1235
other = as_datetime (other )
@@ -1234,6 +1261,7 @@ class YearEnd(YearOffset):
1234
1261
_default_month = 12
1235
1262
_prefix = 'A'
1236
1263
1264
+ @apply_nat
1237
1265
def apply (self , other ):
1238
1266
def _increment (date ):
1239
1267
if date .month == self .month :
@@ -1290,6 +1318,7 @@ class YearBegin(YearOffset):
1290
1318
_default_month = 1
1291
1319
_prefix = 'AS'
1292
1320
1321
+ @apply_nat
1293
1322
def apply (self , other ):
1294
1323
def _increment (date ):
1295
1324
year = date .year
@@ -1410,6 +1439,7 @@ def onOffset(self, dt):
1410
1439
else :
1411
1440
return year_end == dt
1412
1441
1442
+ @apply_nat
1413
1443
def apply (self , other ):
1414
1444
n = self .n
1415
1445
prev_year = self .get_year_end (
@@ -1596,6 +1626,7 @@ def __init__(self, n=1, **kwds):
1596
1626
def isAnchored (self ):
1597
1627
return self .n == 1 and self ._offset .isAnchored ()
1598
1628
1629
+ @apply_nat
1599
1630
def apply (self , other ):
1600
1631
other = as_datetime (other )
1601
1632
n = self .n
@@ -1693,6 +1724,7 @@ class Easter(DateOffset):
1693
1724
def __init__ (self , n = 1 , ** kwds ):
1694
1725
super (Easter , self ).__init__ (n , ** kwds )
1695
1726
1727
+ @apply_nat
1696
1728
def apply (self , other ):
1697
1729
1698
1730
currentEaster = easter (other .year )
@@ -1786,6 +1818,7 @@ def delta(self):
1786
1818
def nanos (self ):
1787
1819
return _delta_to_nanoseconds (self .delta )
1788
1820
1821
+ @apply_nat
1789
1822
def apply (self , other ):
1790
1823
if type (other ) == date :
1791
1824
other = datetime (other .year , other .month , other .day )
0 commit comments