|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +from datetime import date, datetime, timedelta |
2 | 3 | import functools |
3 | 4 | import operator |
4 | 5 |
|
5 | | -from datetime import date, datetime, timedelta |
6 | 6 | from pandas.compat import range |
7 | 7 | from pandas import compat |
8 | 8 | import numpy as np |
@@ -166,7 +166,7 @@ def __add__(date): |
166 | 166 | normalize = False |
167 | 167 |
|
168 | 168 | def __init__(self, n=1, normalize=False, **kwds): |
169 | | - self.n = int(n) |
| 169 | + self.n = self._validate_n(n) |
170 | 170 | self.normalize = normalize |
171 | 171 | self.kwds = kwds |
172 | 172 |
|
@@ -473,7 +473,7 @@ class BusinessDay(BusinessMixin, SingleConstructorOffset): |
473 | 473 | _adjust_dst = True |
474 | 474 |
|
475 | 475 | def __init__(self, n=1, normalize=False, offset=timedelta(0)): |
476 | | - self.n = int(n) |
| 476 | + self.n = self._validate_n(n) |
477 | 477 | self.normalize = normalize |
478 | 478 | self.kwds = {'offset': offset} |
479 | 479 | self._offset = offset |
@@ -782,7 +782,7 @@ class BusinessHour(BusinessHourMixin, SingleConstructorOffset): |
782 | 782 |
|
783 | 783 | def __init__(self, n=1, normalize=False, start='09:00', |
784 | 784 | end='17:00', offset=timedelta(0)): |
785 | | - self.n = int(n) |
| 785 | + self.n = self._validate_n(n) |
786 | 786 | self.normalize = normalize |
787 | 787 | super(BusinessHour, self).__init__(start=start, end=end, offset=offset) |
788 | 788 |
|
@@ -819,7 +819,7 @@ class CustomBusinessDay(BusinessDay): |
819 | 819 |
|
820 | 820 | def __init__(self, n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', |
821 | 821 | holidays=None, calendar=None, offset=timedelta(0)): |
822 | | - self.n = int(n) |
| 822 | + self.n = self._validate_n(n) |
823 | 823 | self.normalize = normalize |
824 | 824 | self._offset = offset |
825 | 825 | self.kwds = {} |
@@ -887,7 +887,7 @@ class CustomBusinessHour(BusinessHourMixin, SingleConstructorOffset): |
887 | 887 | def __init__(self, n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', |
888 | 888 | holidays=None, calendar=None, |
889 | 889 | start='09:00', end='17:00', offset=timedelta(0)): |
890 | | - self.n = int(n) |
| 890 | + self.n = self._validate_n(n) |
891 | 891 | self.normalize = normalize |
892 | 892 | super(CustomBusinessHour, self).__init__(start=start, |
893 | 893 | end=end, offset=offset) |
@@ -919,6 +919,11 @@ def next_bday(self): |
919 | 919 | class MonthOffset(SingleConstructorOffset): |
920 | 920 | _adjust_dst = True |
921 | 921 |
|
| 922 | + def __init__(self, n=1, normalize=False): |
| 923 | + self.n = self._validate_n(n) |
| 924 | + self.normalize = normalize |
| 925 | + self.kwds = {} |
| 926 | + |
922 | 927 | @property |
923 | 928 | def name(self): |
924 | 929 | if self.isAnchored: |
@@ -994,7 +999,8 @@ def __init__(self, n=1, normalize=False, day_of_month=None): |
994 | 999 | msg = 'day_of_month must be {min}<=day_of_month<=27, got {day}' |
995 | 1000 | raise ValueError(msg.format(min=self._min_day_of_month, |
996 | 1001 | day=self.day_of_month)) |
997 | | - self.n = int(n) |
| 1002 | + |
| 1003 | + self.n = self._validate_n(n) |
998 | 1004 | self.normalize = normalize |
999 | 1005 | self.kwds = {'day_of_month': self.day_of_month} |
1000 | 1006 |
|
@@ -1205,7 +1211,7 @@ class CustomBusinessMonthEnd(BusinessMixin, MonthOffset): |
1205 | 1211 |
|
1206 | 1212 | def __init__(self, n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', |
1207 | 1213 | holidays=None, calendar=None, offset=timedelta(0)): |
1208 | | - self.n = int(n) |
| 1214 | + self.n = self._validate_n(n) |
1209 | 1215 | self.normalize = normalize |
1210 | 1216 | self._offset = offset |
1211 | 1217 | self.kwds = {} |
@@ -1278,7 +1284,7 @@ class CustomBusinessMonthBegin(BusinessMixin, MonthOffset): |
1278 | 1284 |
|
1279 | 1285 | def __init__(self, n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', |
1280 | 1286 | holidays=None, calendar=None, offset=timedelta(0)): |
1281 | | - self.n = int(n) |
| 1287 | + self.n = self._validate_n(n) |
1282 | 1288 | self.normalize = normalize |
1283 | 1289 | self._offset = offset |
1284 | 1290 | self.kwds = {} |
@@ -1345,7 +1351,7 @@ class Week(EndMixin, DateOffset): |
1345 | 1351 | _prefix = 'W' |
1346 | 1352 |
|
1347 | 1353 | def __init__(self, n=1, normalize=False, weekday=None): |
1348 | | - self.n = n |
| 1354 | + self.n = self._validate_n(n) |
1349 | 1355 | self.normalize = normalize |
1350 | 1356 | self.weekday = weekday |
1351 | 1357 |
|
@@ -1424,7 +1430,7 @@ class WeekOfMonth(DateOffset): |
1424 | 1430 | _adjust_dst = True |
1425 | 1431 |
|
1426 | 1432 | def __init__(self, n=1, normalize=False, week=None, weekday=None): |
1427 | | - self.n = n |
| 1433 | + self.n = self._validate_n(n) |
1428 | 1434 | self.normalize = normalize |
1429 | 1435 | self.weekday = weekday |
1430 | 1436 | self.week = week |
@@ -1509,7 +1515,7 @@ class LastWeekOfMonth(DateOffset): |
1509 | 1515 | _prefix = 'LWOM' |
1510 | 1516 |
|
1511 | 1517 | def __init__(self, n=1, normalize=False, weekday=None): |
1512 | | - self.n = n |
| 1518 | + self.n = self._validate_n(n) |
1513 | 1519 | self.normalize = normalize |
1514 | 1520 | self.weekday = weekday |
1515 | 1521 |
|
@@ -1575,7 +1581,7 @@ class QuarterOffset(DateOffset): |
1575 | 1581 | # point |
1576 | 1582 |
|
1577 | 1583 | def __init__(self, n=1, normalize=False, startingMonth=None): |
1578 | | - self.n = n |
| 1584 | + self.n = self._validate_n(n) |
1579 | 1585 | self.normalize = normalize |
1580 | 1586 | if startingMonth is None: |
1581 | 1587 | startingMonth = self._default_startingMonth |
@@ -1820,7 +1826,7 @@ class FY5253(DateOffset): |
1820 | 1826 |
|
1821 | 1827 | def __init__(self, n=1, normalize=False, weekday=0, startingMonth=1, |
1822 | 1828 | variation="nearest"): |
1823 | | - self.n = n |
| 1829 | + self.n = self._validate_n(n) |
1824 | 1830 | self.normalize = normalize |
1825 | 1831 | self.startingMonth = startingMonth |
1826 | 1832 | self.weekday = weekday |
@@ -2032,7 +2038,7 @@ class FY5253Quarter(DateOffset): |
2032 | 2038 |
|
2033 | 2039 | def __init__(self, n=1, normalize=False, weekday=0, startingMonth=1, |
2034 | 2040 | qtr_with_extra_week=1, variation="nearest"): |
2035 | | - self.n = n |
| 2041 | + self.n = self._validate_n(n) |
2036 | 2042 | self.normalize = normalize |
2037 | 2043 |
|
2038 | 2044 | self.weekday = weekday |
@@ -2158,6 +2164,11 @@ class Easter(DateOffset): |
2158 | 2164 | """ |
2159 | 2165 | _adjust_dst = True |
2160 | 2166 |
|
| 2167 | + def __init__(self, n=1, normalize=False): |
| 2168 | + self.n = self._validate_n(n) |
| 2169 | + self.normalize = normalize |
| 2170 | + self.kwds = {} |
| 2171 | + |
2161 | 2172 | @apply_wraps |
2162 | 2173 | def apply(self, other): |
2163 | 2174 | current_easter = easter(other.year) |
@@ -2199,6 +2210,12 @@ class Tick(SingleConstructorOffset): |
2199 | 2210 | _inc = Timedelta(microseconds=1000) |
2200 | 2211 | _prefix = 'undefined' |
2201 | 2212 |
|
| 2213 | + def __init__(self, n=1, normalize=False): |
| 2214 | + # TODO: do Tick classes with normalize=True make sense? |
| 2215 | + self.n = self._validate_n(n) |
| 2216 | + self.normalize = normalize |
| 2217 | + self.kwds = {} |
| 2218 | + |
2202 | 2219 | __gt__ = _tick_comp(operator.gt) |
2203 | 2220 | __ge__ = _tick_comp(operator.ge) |
2204 | 2221 | __lt__ = _tick_comp(operator.lt) |
@@ -2257,6 +2274,7 @@ def delta(self): |
2257 | 2274 | def nanos(self): |
2258 | 2275 | return delta_to_nanoseconds(self.delta) |
2259 | 2276 |
|
| 2277 | + # TODO: Should Tick have its own apply_index? |
2260 | 2278 | def apply(self, other): |
2261 | 2279 | # Timestamp can handle tz and nano sec, thus no need to use apply_wraps |
2262 | 2280 | if isinstance(other, Timestamp): |
|
0 commit comments