diff --git a/doc/source/release.rst b/doc/source/release.rst index af59137a194b0..b35f8298c815a 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -638,6 +638,8 @@ Bug Fixes which doesn't have a row for index names (:issue:`4702`) - Bug when trying to use an out-of-bounds date as an object dtype (:issue:`5312`) - Bug when trying to display an embedded PandasObject (:issue:`5324`) + - Allows operating of Timestamps to return a datetime if the result is out-of-bounds + related (:issue:`5312`) pandas 0.12.0 ------------- diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 07efbcfdcd7ba..8830d66b245ef 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -8,17 +8,32 @@ # import after tools, dateutil check from dateutil.relativedelta import relativedelta, weekday import pandas.tslib as tslib -from pandas.tslib import Timestamp +from pandas.tslib import Timestamp, OutOfBoundsDatetime + from pandas import _np_version_under1p7 __all__ = ['Day', 'BusinessDay', 'BDay', 'CustomBusinessDay', 'CDay', 'MonthBegin', 'BMonthBegin', 'MonthEnd', 'BMonthEnd', 'YearBegin', 'BYearBegin', 'YearEnd', 'BYearEnd', 'QuarterBegin', 'BQuarterBegin', 'QuarterEnd', 'BQuarterEnd', - 'LastWeekOfMonth', 'FY5253Quarter', 'FY5253', + 'LastWeekOfMonth', 'FY5253Quarter', 'FY5253', 'Week', 'WeekOfMonth', 'Hour', 'Minute', 'Second', 'Milli', 'Micro', 'Nano'] +# convert to/from datetime/timestamp to allow invalid Timestamp ranges to pass thru +def as_timestamp(obj): + try: + return Timestamp(obj) + except (OutOfBoundsDatetime): + pass + return obj + +def as_datetime(obj): + f = getattr(obj,'to_pydatetime',None) + if f is not None: + obj = f() + return obj + #---------------------------------------------------------------------- # DateOffset @@ -87,6 +102,7 @@ def __init__(self, n=1, **kwds): self._offset = timedelta(1) def apply(self, other): + other = as_datetime(other) if len(self.kwds) > 0: if self.n > 0: for i in range(self.n): @@ -94,9 +110,9 @@ def apply(self, other): else: for i in range(-self.n): other = other - self._offset - return Timestamp(other) + return as_timestamp(other) else: - return Timestamp(other + timedelta(self.n)) + return as_timestamp(other + timedelta(self.n)) def isAnchored(self): return (self.n == 1) @@ -394,7 +410,7 @@ def apply(self, other): if self.offset: result = result + self.offset - return Timestamp(result) + return as_timestamp(result) elif isinstance(other, (timedelta, Tick)): return BDay(self.n, offset=self.offset + other, @@ -540,11 +556,11 @@ def apply(self, other): n = self.n _, days_in_month = tslib.monthrange(other.year, other.month) if other.day != days_in_month: - other = other + relativedelta(months=-1, day=31) + other = as_datetime(other) + relativedelta(months=-1, day=31) if n <= 0: n = n + 1 - other = other + relativedelta(months=n, day=31) - return Timestamp(other) + other = as_datetime(other) + relativedelta(months=n, day=31) + return as_timestamp(other) @classmethod def onOffset(cls, dt): @@ -563,8 +579,8 @@ def apply(self, other): if other.day > 1 and n <= 0: # then roll forward if n<=0 n += 1 - other = other + relativedelta(months=n, day=1) - return Timestamp(other) + other = as_datetime(other) + relativedelta(months=n, day=1) + return as_timestamp(other) @classmethod def onOffset(cls, dt): @@ -592,7 +608,7 @@ def apply(self, other): n = n - 1 elif n <= 0 and other.day > lastBDay: n = n + 1 - other = other + relativedelta(months=n, day=31) + other = as_datetime(other) + relativedelta(months=n, day=31) if other.weekday() > 4: other = other - BDay() @@ -614,14 +630,14 @@ def apply(self, other): # as if rolled forward already n += 1 elif other.day < first and n > 0: - other = other + timedelta(days=first - other.day) + other = as_datetime(other) + timedelta(days=first - other.day) n -= 1 - other = other + relativedelta(months=n) + other = as_datetime(other) + relativedelta(months=n) wkday, _ = tslib.monthrange(other.year, other.month) first = _get_firstbday(wkday) result = datetime(other.year, other.month, first) - return result + return as_timestamp(result) @classmethod def onOffset(cls, dt): @@ -663,24 +679,26 @@ def isAnchored(self): def apply(self, other): if self.weekday is None: - return other + self.n * self._inc + return as_datetime(other) + self.n * self._inc if self.n > 0: k = self.n otherDay = other.weekday() if otherDay != self.weekday: - other = other + timedelta((self.weekday - otherDay) % 7) + other = as_datetime(other) + timedelta((self.weekday - otherDay) % 7) k = k - 1 + other = as_datetime(other) for i in range(k): other = other + self._inc else: k = self.n otherDay = other.weekday() if otherDay != self.weekday: - other = other + timedelta((self.weekday - otherDay) % 7) + other = as_datetime(other) + timedelta((self.weekday - otherDay) % 7) + other = as_datetime(other) for i in range(-k): other = other - self._inc - return Timestamp(other) + return as_timestamp(other) def onOffset(self, dt): return dt.weekday() == self.weekday @@ -709,7 +727,7 @@ class WeekDay(object): THU = 3 FRI = 4 SAT = 5 - SUN = 6 + SUN = 6 _int_to_weekday = { WeekDay.MON: 'MON', @@ -776,8 +794,8 @@ def apply(self, other): else: months = self.n + 1 - return self.getOffsetOfMonth(other + relativedelta(months=months, - day=1)) + return self.getOffsetOfMonth(as_datetime(other) + relativedelta(months=months, + day=1)) def getOffsetOfMonth(self, dt): w = Week(weekday=self.weekday) @@ -809,7 +827,7 @@ def _from_name(cls, suffix=None): week = int(suffix[0]) - 1 weekday = _weekday_to_int[suffix[1:]] return cls(week=week, weekday=weekday) - + class LastWeekOfMonth(CacheableOffset, DateOffset): """ Describes monthly dates in last week of month like "the last Tuesday of each month" @@ -855,16 +873,16 @@ def apply(self, other): else: months = self.n + 1 - return self.getOffsetOfMonth(other + relativedelta(months=months, day=1)) + return self.getOffsetOfMonth(as_datetime(other) + relativedelta(months=months, day=1)) def getOffsetOfMonth(self, dt): m = MonthEnd() d = datetime(dt.year, dt.month, 1) eom = m.rollforward(d) - + w = Week(weekday=self.weekday) - + return w.rollback(eom) def onOffset(self, dt): @@ -948,7 +966,7 @@ def apply(self, other): elif n <= 0 and other.day > lastBDay and monthsToGo == 0: n = n + 1 - other = other + relativedelta(months=monthsToGo + 3 * n, day=31) + other = as_datetime(other) + relativedelta(months=monthsToGo + 3 * n, day=31) if other.weekday() > 4: other = other - BDay() @@ -962,22 +980,22 @@ def onOffset(self, dt): class FY5253(CacheableOffset, DateOffset): """ Describes 52-53 week fiscal year. This is also known as a 4-4-5 calendar. - - It is used by companies that desire that their + + It is used by companies that desire that their fiscal year always end on the same day of the week. - - It is a method of managing accounting periods. - It is a common calendar structure for some industries, + + It is a method of managing accounting periods. + It is a common calendar structure for some industries, such as retail, manufacturing and parking industry. - - For more information see: + + For more information see: http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar - - - The year may either: + + + The year may either: - end on the last X day of the Y month. - end on the last X day closest to the last day of the Y month. - + X is a specific day of the week. Y is a certain month of the year @@ -996,41 +1014,41 @@ class FY5253(CacheableOffset, DateOffset): variation : str {"nearest", "last"} for "LastOfMonth" or "NearestEndMonth" """ - + _prefix = 'RE' _suffix_prefix_last = 'L' _suffix_prefix_nearest = 'N' - + def __init__(self, n=1, **kwds): self.n = n self.startingMonth = kwds['startingMonth'] self.weekday = kwds["weekday"] - + self.variation = kwds["variation"] - + self.kwds = kwds - + if self.n == 0: raise ValueError('N cannot be 0') - + if self.variation not in ["nearest", "last"]: raise ValueError('%s is not a valid variation' % self.variation) - + if self.variation == "nearest": self._rd_forward = relativedelta(weekday=weekday(self.weekday)) self._rd_backward = relativedelta(weekday=weekday(self.weekday)(-1)) else: - self._offset_lwom = LastWeekOfMonth(n=1, weekday=self.weekday) - + self._offset_lwom = LastWeekOfMonth(n=1, weekday=self.weekday) + def isAnchored(self): return self.n == 1 \ and self.startingMonth is not None \ - and self.weekday is not None - + and self.weekday is not None + def onOffset(self, dt): year_end = self.get_year_end(dt) return year_end == dt - + def apply(self, other): n = self.n if n > 0: @@ -1039,10 +1057,10 @@ def apply(self, other): other = year_end n -= 1 elif other > year_end: - other = self.get_year_end(other + relativedelta(years=1)) + other = self.get_year_end(as_datetime(other) + relativedelta(years=1)) n -= 1 - - return self.get_year_end(other + relativedelta(years=n)) + + return self.get_year_end(as_datetime(other) + relativedelta(years=n)) else: n = -n year_end = self.get_year_end(other) @@ -1050,10 +1068,10 @@ def apply(self, other): other = year_end n -= 1 elif other < year_end: - other = self.get_year_end(other + relativedelta(years=-1)) + other = self.get_year_end(as_datetime(other) + relativedelta(years=-1)) n -= 1 - - return self.get_year_end(other + relativedelta(years=-n)) + + return self.get_year_end(as_datetime(other) + relativedelta(years=-n)) def get_year_end(self, dt): if self.variation == "nearest": @@ -1065,7 +1083,7 @@ def get_target_month_end(self, dt): target_month = datetime(year=dt.year, month=self.startingMonth, day=1) next_month_first_of = target_month + relativedelta(months=+1) return next_month_first_of + relativedelta(days=-1) - + def _get_year_end_nearest(self, dt): target_date = self.get_target_month_end(dt) if target_date.weekday() == self.weekday: @@ -1073,21 +1091,21 @@ def _get_year_end_nearest(self, dt): else: forward = target_date + self._rd_forward backward = target_date + self._rd_backward - + if forward - target_date < target_date - backward: return forward else: return backward - + def _get_year_end_last(self, dt): current_year = datetime(year=dt.year, month=self.startingMonth, day=1) return current_year + self._offset_lwom - + @property def rule_code(self): suffix = self.get_rule_code_suffix() return "%s-%s" % (self._get_prefix(), suffix) - + def _get_prefix(self): return self._prefix @@ -1096,12 +1114,12 @@ def _get_suffix_prefix(self): return self._suffix_prefix_nearest else: return self._suffix_prefix_last - + def get_rule_code_suffix(self): return '%s-%s-%s' % (self._get_suffix_prefix(), \ _int_to_month[self.startingMonth], \ _int_to_weekday[self.weekday]) - + @classmethod def _parse_suffix(cls, varion_code, startingMonth_code, weekday_code): if varion_code == "N": @@ -1110,42 +1128,42 @@ def _parse_suffix(cls, varion_code, startingMonth_code, weekday_code): variation = "last" else: raise ValueError("Unable to parse varion_code: %s" % (varion_code,)) - + startingMonth = _month_to_int[startingMonth_code] weekday = _weekday_to_int[weekday_code] - + return { - "weekday":weekday, + "weekday":weekday, "startingMonth":startingMonth, "variation":variation, } - + @classmethod def _from_name(cls, *args): return cls(**cls._parse_suffix(*args)) - + class FY5253Quarter(CacheableOffset, DateOffset): """ DateOffset increments between business quarter dates for 52-53 week fiscal year (also known as a 4-4-5 calendar). - - It is used by companies that desire that their + + It is used by companies that desire that their fiscal year always end on the same day of the week. - - It is a method of managing accounting periods. - It is a common calendar structure for some industries, + + It is a method of managing accounting periods. + It is a common calendar structure for some industries, such as retail, manufacturing and parking industry. - - For more information see: + + For more information see: http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar - - The year may either: + + The year may either: - end on the last X day of the Y month. - end on the last X day closest to the last day of the Y month. - + X is a specific day of the week. Y is a certain month of the year - + startingMonth = 1 corresponds to dates like 1/31/2007, 4/30/2007, ... startingMonth = 2 corresponds to dates like 2/28/2007, 5/31/2007, ... startingMonth = 3 corresponds to dates like 3/30/2007, 6/29/2007, ... @@ -1162,35 +1180,36 @@ class FY5253Quarter(CacheableOffset, DateOffset): 5: Saturdays 6: Sundays startingMonth : The month in which fiscal years end. {1, 2, ... 12} - qtr_with_extra_week : The quarter number that has the leap + qtr_with_extra_week : The quarter number that has the leap or 14 week when needed. {1,2,3,4} variation : str - {"nearest", "last"} for "LastOfMonth" or "NearestEndMonth" + {"nearest", "last"} for "LastOfMonth" or "NearestEndMonth" """ - + _prefix = 'REQ' - + def __init__(self, n=1, **kwds): self.n = n - + self.qtr_with_extra_week = kwds["qtr_with_extra_week"] self.kwds = kwds if self.n == 0: raise ValueError('N cannot be 0') - + self._offset = FY5253( \ startingMonth=kwds['startingMonth'], \ weekday=kwds["weekday"], variation=kwds["variation"]) - + def isAnchored(self): return self.n == 1 and self._offset.isAnchored() def apply(self, other): + other = as_datetime(other) n = self.n - + if n > 0: while n > 0: if not self._offset.onOffset(other): @@ -1199,14 +1218,14 @@ def apply(self, other): else: start = other qtr_lens = self.get_weeks(other + self._offset) - + for weeks in qtr_lens: start += relativedelta(weeks=weeks) if start > other: other = start n -= 1 break - + else: n = -n while n > 0: @@ -1216,7 +1235,7 @@ def apply(self, other): else: end = other qtr_lens = self.get_weeks(other) - + for weeks in reversed(qtr_lens): end -= relativedelta(weeks=weeks) if end < other: @@ -1225,53 +1244,53 @@ def apply(self, other): break return other - + def get_weeks(self, dt): ret = [13] * 4 year_has_extra_week = self.year_has_extra_week(dt) - + if year_has_extra_week: ret[self.qtr_with_extra_week-1] = 14 - + return ret - + def year_has_extra_week(self, dt): if self._offset.onOffset(dt): prev_year_end = dt - self._offset next_year_end = dt - else: + else: next_year_end = dt + self._offset prev_year_end = dt - self._offset - + week_in_year = (next_year_end - prev_year_end).days/7 - return week_in_year == 53 - + return week_in_year == 53 + def onOffset(self, dt): if self._offset.onOffset(dt): return True - + next_year_end = dt - self._offset qtr_lens = self.get_weeks(dt) - + current = next_year_end for qtr_len in qtr_lens[0:4]: current += relativedelta(weeks=qtr_len) if dt == current: return True return False - + @property def rule_code(self): suffix = self._offset.get_rule_code_suffix() return "%s-%s" %(self._prefix, "%s-%d" % (suffix, self.qtr_with_extra_week)) - + @classmethod def _from_name(cls, *args): return cls(**dict(FY5253._parse_suffix(*args[:-1]), qtr_with_extra_week=int(args[-1]))) - + _int_to_month = { 1: 'JAN', 2: 'FEB', @@ -1300,6 +1319,7 @@ class BQuarterBegin(CacheableOffset, QuarterOffset): def apply(self, other): n = self.n + other = as_datetime(other) wkday, _ = tslib.monthrange(other.year, other.month) @@ -1324,7 +1344,7 @@ def apply(self, other): result = datetime(other.year, other.month, first, other.hour, other.minute, other.second, other.microsecond) - return result + return as_timestamp(result) class QuarterEnd(CacheableOffset, QuarterOffset): @@ -1348,6 +1368,7 @@ def isAnchored(self): def apply(self, other): n = self.n + other = as_datetime(other) wkday, days_in_month = tslib.monthrange(other.year, other.month) @@ -1360,7 +1381,7 @@ def apply(self, other): other = other + relativedelta(months=monthsToGo + 3 * n, day=31) - return Timestamp(other) + return as_timestamp(other) def onOffset(self, dt): modMonth = (dt.month - self.startingMonth) % 3 @@ -1378,6 +1399,7 @@ def isAnchored(self): def apply(self, other): n = self.n + other = as_datetime(other) wkday, days_in_month = tslib.monthrange(other.year, other.month) @@ -1392,7 +1414,7 @@ def apply(self, other): n = n + 1 other = other + relativedelta(months=3 * n - monthsSince, day=1) - return Timestamp(other) + return as_timestamp(other) class YearOffset(DateOffset): @@ -1426,6 +1448,7 @@ class BYearEnd(CacheableOffset, YearOffset): def apply(self, other): n = self.n + other = as_datetime(other) wkday, days_in_month = tslib.monthrange(other.year, self.month) lastBDay = (days_in_month - @@ -1462,6 +1485,7 @@ class BYearBegin(CacheableOffset, YearOffset): def apply(self, other): n = self.n + other = as_datetime(other) wkday, days_in_month = tslib.monthrange(other.year, self.month) @@ -1482,7 +1506,7 @@ def apply(self, other): other = other + relativedelta(years=years) wkday, days_in_month = tslib.monthrange(other.year, self.month) first = _get_firstbday(wkday) - return datetime(other.year, self.month, first) + return as_timestamp(datetime(other.year, self.month, first)) class YearEnd(CacheableOffset, YearOffset): @@ -1534,7 +1558,7 @@ def _rollf(date): # n == 0, roll forward result = _rollf(result) - return Timestamp(result) + return as_timestamp(result) def onOffset(self, dt): wkday, days_in_month = tslib.monthrange(dt.year, self.month) @@ -1581,7 +1605,7 @@ def _rollf(date): # n == 0, roll forward result = _rollf(result) - return Timestamp(result) + return as_timestamp(result) def onOffset(self, dt): return dt.month == self.month and dt.day == 1 @@ -1848,7 +1872,7 @@ def generate_range(start=None, end=None, periods=None, Day, # 'D' WeekOfMonth, # 'WOM' FY5253, - FY5253Quarter, + FY5253Quarter, ]) if not _np_version_under1p7: diff --git a/pandas/tseries/tests/test_offsets.py b/pandas/tseries/tests/test_offsets.py index 7ebe6c0cfb728..f66f57cc45409 100644 --- a/pandas/tseries/tests/test_offsets.py +++ b/pandas/tseries/tests/test_offsets.py @@ -21,7 +21,7 @@ from pandas.tseries.tools import parse_time_string import pandas.tseries.offsets as offsets -from pandas.tslib import monthrange +from pandas.tslib import monthrange, OutOfBoundsDatetime from pandas.lib import Timestamp from pandas.util.testing import assertRaisesRegexp import pandas.util.testing as tm @@ -95,8 +95,26 @@ def test_to_m8(): ### DateOffset Tests ##### +class TestBase(unittest.TestCase): + _offset = None -class TestDateOffset(unittest.TestCase): + def test_apply_out_of_range(self): + if self._offset is None: + raise nose.SkipTest("_offset not defined") + + # try to create an out-of-bounds result timestamp; if we can't create the offset + # skip + try: + offset = self._offset(10000) + + result = Timestamp('20080101') + offset + self.assert_(isinstance(result, datetime)) + except (OutOfBoundsDatetime): + raise + except (ValueError, KeyError): + raise nose.SkipTest("cannot create out_of_range offset") + +class TestDateOffset(TestBase): _multiprocess_can_split_ = True def setUp(self): @@ -137,8 +155,9 @@ def test_eq(self): self.assert_(not (offset1 == offset2)) -class TestBusinessDay(unittest.TestCase): +class TestBusinessDay(TestBase): _multiprocess_can_split_ = True + _offset = BDay def setUp(self): self.d = datetime(2008, 1, 1) @@ -310,8 +329,9 @@ def test_offsets_compare_equal(self): self.assertFalse(offset1 != offset2) -class TestCustomBusinessDay(unittest.TestCase): +class TestCustomBusinessDay(TestBase): _multiprocess_can_split_ = True + _offset = CDay def setUp(self): self.d = datetime(2008, 1, 1) @@ -531,7 +551,9 @@ def assertOnOffset(offset, date, expected): (expected, actual, offset, date)) -class TestWeek(unittest.TestCase): +class TestWeek(TestBase): + _offset = Week + def test_repr(self): self.assertEqual(repr(Week(weekday=0)), "") self.assertEqual(repr(Week(n=-1, weekday=0)), "<-1 * Week: weekday=0>") @@ -600,7 +622,8 @@ def test_offsets_compare_equal(self): self.assertFalse(offset1 != offset2) -class TestWeekOfMonth(unittest.TestCase): +class TestWeekOfMonth(TestBase): + _offset = WeekOfMonth def test_constructor(self): assertRaisesRegexp(ValueError, "^N cannot be 0", WeekOfMonth, n=0, week=1, weekday=1) @@ -678,11 +701,13 @@ def test_onOffset(self): offset = WeekOfMonth(week=week, weekday=weekday) self.assert_(offset.onOffset(date) == expected) -class TestLastWeekOfMonth(unittest.TestCase): +class TestLastWeekOfMonth(TestBase): + _offset = LastWeekOfMonth + def test_constructor(self): assertRaisesRegexp(ValueError, "^N cannot be 0", \ LastWeekOfMonth, n=0, weekday=1) - + assertRaisesRegexp(ValueError, "^Day", LastWeekOfMonth, n=1, weekday=-1) assertRaisesRegexp(ValueError, "^Day", LastWeekOfMonth, n=1, weekday=7) @@ -691,40 +716,40 @@ def test_offset(self): last_sat = datetime(2013,8,31) next_sat = datetime(2013,9,28) offset_sat = LastWeekOfMonth(n=1, weekday=5) - - one_day_before = (last_sat + timedelta(days=-1)) + + one_day_before = (last_sat + timedelta(days=-1)) self.assert_(one_day_before + offset_sat == last_sat) - + one_day_after = (last_sat + timedelta(days=+1)) self.assert_(one_day_after + offset_sat == next_sat) - + #Test On that day self.assert_(last_sat + offset_sat == next_sat) - + #### Thursday - + offset_thur = LastWeekOfMonth(n=1, weekday=3) last_thurs = datetime(2013,1,31) next_thurs = datetime(2013,2,28) - + one_day_before = last_thurs + timedelta(days=-1) self.assert_(one_day_before + offset_thur == last_thurs) - + one_day_after = last_thurs + timedelta(days=+1) self.assert_(one_day_after + offset_thur == next_thurs) - + # Test on that day self.assert_(last_thurs + offset_thur == next_thurs) - + three_before = last_thurs + timedelta(days=-3) self.assert_(three_before + offset_thur == last_thurs) - + two_after = last_thurs + timedelta(days=+2) self.assert_(two_after + offset_thur == next_thurs) - + offset_sunday = LastWeekOfMonth(n=1, weekday=WeekDay.SUN) self.assert_(datetime(2013,7,31) + offset_sunday == datetime(2013,8,25)) - + def test_onOffset(self): test_cases = [ (WeekDay.SUN, datetime(2013, 1, 27), True), @@ -733,7 +758,7 @@ def test_onOffset(self): (WeekDay.SUN, datetime(2013, 2, 25), False), #Not a SUN (WeekDay.MON, datetime(2013, 2, 25), True), (WeekDay.SAT, datetime(2013, 11, 30), True), - + (WeekDay.SAT, datetime(2006, 8, 26), True), (WeekDay.SAT, datetime(2007, 8, 25), True), (WeekDay.SAT, datetime(2008, 8, 30), True), @@ -742,13 +767,15 @@ def test_onOffset(self): (WeekDay.SAT, datetime(2011, 8, 27), True), (WeekDay.SAT, datetime(2019, 8, 31), True), ] - + for weekday, date, expected in test_cases: offset = LastWeekOfMonth(weekday=weekday) self.assert_(offset.onOffset(date) == expected, date) -class TestBMonthBegin(unittest.TestCase): +class TestBMonthBegin(TestBase): + _offset = BMonthBegin + def test_offset(self): tests = [] @@ -808,7 +835,8 @@ def test_offsets_compare_equal(self): self.assertFalse(offset1 != offset2) -class TestBMonthEnd(unittest.TestCase): +class TestBMonthEnd(TestBase): + _offset = BMonthEnd def test_offset(self): tests = [] @@ -870,7 +898,8 @@ def test_offsets_compare_equal(self): self.assertFalse(offset1 != offset2) -class TestMonthBegin(unittest.TestCase): +class TestMonthBegin(TestBase): + _offset = MonthBegin def test_offset(self): tests = [] @@ -910,7 +939,8 @@ def test_offset(self): assertEq(offset, base, expected) -class TestMonthEnd(unittest.TestCase): +class TestMonthEnd(TestBase): + _offset = MonthEnd def test_offset(self): tests = [] @@ -977,7 +1007,8 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestBQuarterBegin(unittest.TestCase): +class TestBQuarterBegin(TestBase): + _offset = BQuarterBegin def test_repr(self): self.assertEqual(repr(BQuarterBegin()),"") @@ -1067,7 +1098,8 @@ def test_offset(self): self.assertEqual(datetime(2007, 4, 3) + offset, datetime(2007, 4, 2)) -class TestBQuarterEnd(unittest.TestCase): +class TestBQuarterEnd(TestBase): + _offset = BQuarterEnd def test_repr(self): self.assertEqual(repr(BQuarterEnd()),"") @@ -1185,13 +1217,14 @@ def makeFY5253NearestEndMonth(*args, **kwds): def makeFY5253LastOfMonth(*args, **kwds): return FY5253(*args, variation="last", **kwds) - -class TestFY5253LastOfMonth(unittest.TestCase): + +class TestFY5253LastOfMonth(TestBase): + def test_onOffset(self): offset_lom_sat_aug = makeFY5253LastOfMonth(1, startingMonth=8, weekday=WeekDay.SAT) offset_lom_sat_sep = makeFY5253LastOfMonth(1, startingMonth=9, weekday=WeekDay.SAT) - + tests = [ #From Wikipedia (see: http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar#Last_Saturday_of_the_month_at_fiscal_year_end) (offset_lom_sat_aug, datetime(2006, 8, 26), True), @@ -1208,43 +1241,43 @@ def test_onOffset(self): (offset_lom_sat_aug, datetime(2017, 8, 26), True), (offset_lom_sat_aug, datetime(2018, 8, 25), True), (offset_lom_sat_aug, datetime(2019, 8, 31), True), - + (offset_lom_sat_aug, datetime(2006, 8, 27), False), (offset_lom_sat_aug, datetime(2007, 8, 28), False), (offset_lom_sat_aug, datetime(2008, 8, 31), False), (offset_lom_sat_aug, datetime(2009, 8, 30), False), (offset_lom_sat_aug, datetime(2010, 8, 29), False), (offset_lom_sat_aug, datetime(2011, 8, 28), False), - + (offset_lom_sat_aug, datetime(2006, 8, 25), False), (offset_lom_sat_aug, datetime(2007, 8, 24), False), (offset_lom_sat_aug, datetime(2008, 8, 29), False), (offset_lom_sat_aug, datetime(2009, 8, 28), False), (offset_lom_sat_aug, datetime(2010, 8, 27), False), (offset_lom_sat_aug, datetime(2011, 8, 26), False), - (offset_lom_sat_aug, datetime(2019, 8, 30), False), + (offset_lom_sat_aug, datetime(2019, 8, 30), False), #From GMCR (see for example: http://yahoo.brand.edgar-online.com/Default.aspx?companyid=3184&formtypeID=7) - (offset_lom_sat_sep, datetime(2010, 9, 25), True), - (offset_lom_sat_sep, datetime(2011, 9, 24), True), - (offset_lom_sat_sep, datetime(2012, 9, 29), True), - + (offset_lom_sat_sep, datetime(2010, 9, 25), True), + (offset_lom_sat_sep, datetime(2011, 9, 24), True), + (offset_lom_sat_sep, datetime(2012, 9, 29), True), + ] for offset, date, expected in tests: assertOnOffset(offset, date, expected) - + def test_apply(self): offset_lom_aug_sat = makeFY5253LastOfMonth(startingMonth=8, weekday=WeekDay.SAT) offset_lom_aug_sat_1 = makeFY5253LastOfMonth(n=1, startingMonth=8, weekday=WeekDay.SAT) - - date_seq_lom_aug_sat = [datetime(2006, 8, 26), datetime(2007, 8, 25), - datetime(2008, 8, 30), datetime(2009, 8, 29), - datetime(2010, 8, 28), datetime(2011, 8, 27), - datetime(2012, 8, 25), datetime(2013, 8, 31), - datetime(2014, 8, 30), datetime(2015, 8, 29), + + date_seq_lom_aug_sat = [datetime(2006, 8, 26), datetime(2007, 8, 25), + datetime(2008, 8, 30), datetime(2009, 8, 29), + datetime(2010, 8, 28), datetime(2011, 8, 27), + datetime(2012, 8, 25), datetime(2013, 8, 31), + datetime(2014, 8, 30), datetime(2015, 8, 29), datetime(2016, 8, 27)] - + tests = [ (offset_lom_aug_sat, date_seq_lom_aug_sat), (offset_lom_aug_sat_1, date_seq_lom_aug_sat), @@ -1258,22 +1291,23 @@ def test_apply(self): for datum in data[1:]: current = current + offset self.assertEqual(current, datum) - -class TestFY5253NearestEndMonth(unittest.TestCase): + +class TestFY5253NearestEndMonth(TestBase): + def test_get_target_month_end(self): self.assertEqual(makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.SAT).get_target_month_end(datetime(2013,1,1)), datetime(2013,8,31)) self.assertEqual(makeFY5253NearestEndMonth(startingMonth=12, weekday=WeekDay.SAT).get_target_month_end(datetime(2013,1,1)), datetime(2013,12,31)) self.assertEqual(makeFY5253NearestEndMonth(startingMonth=2, weekday=WeekDay.SAT).get_target_month_end(datetime(2013,1,1)), datetime(2013,2,28)) - + def test_get_year_end(self): self.assertEqual(makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.SAT).get_year_end(datetime(2013,1,1)), datetime(2013,8,31)) self.assertEqual(makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.SUN).get_year_end(datetime(2013,1,1)), datetime(2013,9,1)) self.assertEqual(makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.FRI).get_year_end(datetime(2013,1,1)), datetime(2013,8,30)) - + def test_onOffset(self): offset_lom_aug_sat = makeFY5253NearestEndMonth(1, startingMonth=8, weekday=WeekDay.SAT) offset_lom_aug_thu = makeFY5253NearestEndMonth(1, startingMonth=8, weekday=WeekDay.THU) - + tests = [ # From Wikipedia (see: http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar#Saturday_nearest_the_end_of_month) # 2006-09-02 2006 September 2 @@ -1296,19 +1330,19 @@ def test_onOffset(self): (offset_lom_aug_sat, datetime(2009, 8, 29), True), (offset_lom_aug_sat, datetime(2010, 8, 28), True), (offset_lom_aug_sat, datetime(2011, 9, 3), True), - + (offset_lom_aug_sat, datetime(2016, 9, 3), True), (offset_lom_aug_sat, datetime(2017, 9, 2), True), - (offset_lom_aug_sat, datetime(2018, 9, 1), True), + (offset_lom_aug_sat, datetime(2018, 9, 1), True), (offset_lom_aug_sat, datetime(2019, 8, 31), True), - + (offset_lom_aug_sat, datetime(2006, 8, 27), False), (offset_lom_aug_sat, datetime(2007, 8, 28), False), (offset_lom_aug_sat, datetime(2008, 8, 31), False), (offset_lom_aug_sat, datetime(2009, 8, 30), False), (offset_lom_aug_sat, datetime(2010, 8, 29), False), (offset_lom_aug_sat, datetime(2011, 8, 28), False), - + (offset_lom_aug_sat, datetime(2006, 8, 25), False), (offset_lom_aug_sat, datetime(2007, 8, 24), False), (offset_lom_aug_sat, datetime(2008, 8, 29), False), @@ -1316,19 +1350,19 @@ def test_onOffset(self): (offset_lom_aug_sat, datetime(2010, 8, 27), False), (offset_lom_aug_sat, datetime(2011, 8, 26), False), (offset_lom_aug_sat, datetime(2019, 8, 30), False), - + #From Micron, see: http://google.brand.edgar-online.com/?sym=MU&formtypeID=7 (offset_lom_aug_thu, datetime(2012, 8, 30), True), (offset_lom_aug_thu, datetime(2011, 9, 1), True), - + ] for offset, date, expected in tests: assertOnOffset(offset, date, expected) - + def test_apply(self): date_seq_nem_8_sat = [datetime(2006, 9, 2), datetime(2007, 9, 1), datetime(2008, 8, 30), datetime(2009, 8, 29), datetime(2010, 8, 28), datetime(2011, 9, 3)] - + tests = [ (makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.SAT), date_seq_nem_8_sat), (makeFY5253NearestEndMonth(n=1, startingMonth=8, weekday=WeekDay.SAT), date_seq_nem_8_sat), @@ -1343,68 +1377,68 @@ def test_apply(self): current = current + offset self.assertEqual(current, datum) -class TestFY5253LastOfMonthQuarter(unittest.TestCase): +class TestFY5253LastOfMonthQuarter(TestBase): def test_isAnchored(self): self.assert_(makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4).isAnchored()) self.assert_(makeFY5253LastOfMonthQuarter(weekday=WeekDay.SAT, startingMonth=3, qtr_with_extra_week=4).isAnchored()) self.assert_(not makeFY5253LastOfMonthQuarter(2, startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4).isAnchored()) - + def test_equality(self): self.assertEqual(makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4), makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4)) self.assertNotEqual(makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4), makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SUN, qtr_with_extra_week=4)) self.assertNotEqual(makeFY5253LastOfMonthQuarter(startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4), makeFY5253LastOfMonthQuarter(startingMonth=2, weekday=WeekDay.SAT, qtr_with_extra_week=4)) - + def test_offset(self): offset = makeFY5253LastOfMonthQuarter(1, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) offset2 = makeFY5253LastOfMonthQuarter(2, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) offset4 = makeFY5253LastOfMonthQuarter(4, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) - + offset_neg1 = makeFY5253LastOfMonthQuarter(-1, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) offset_neg2 = makeFY5253LastOfMonthQuarter(-2, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) - - GMCR = [datetime(2010, 3, 27), - datetime(2010, 6, 26), - datetime(2010, 9, 25), - datetime(2010, 12, 25), - datetime(2011, 3, 26), - datetime(2011, 6, 25), - datetime(2011, 9, 24), - datetime(2011, 12, 24), - datetime(2012, 3, 24), - datetime(2012, 6, 23), - datetime(2012, 9, 29), - datetime(2012, 12, 29), - datetime(2013, 3, 30), + + GMCR = [datetime(2010, 3, 27), + datetime(2010, 6, 26), + datetime(2010, 9, 25), + datetime(2010, 12, 25), + datetime(2011, 3, 26), + datetime(2011, 6, 25), + datetime(2011, 9, 24), + datetime(2011, 12, 24), + datetime(2012, 3, 24), + datetime(2012, 6, 23), + datetime(2012, 9, 29), + datetime(2012, 12, 29), + datetime(2013, 3, 30), datetime(2013, 6, 29)] - - assertEq(offset, base=GMCR[0], expected=GMCR[1]) + + assertEq(offset, base=GMCR[0], expected=GMCR[1]) assertEq(offset, base=GMCR[0] + relativedelta(days=-1), expected=GMCR[0]) - assertEq(offset, base=GMCR[1], expected=GMCR[2]) - + assertEq(offset, base=GMCR[1], expected=GMCR[2]) + assertEq(offset2, base=GMCR[0], expected=GMCR[2]) assertEq(offset4, base=GMCR[0], expected=GMCR[4]) - + assertEq(offset_neg1, base=GMCR[-1], expected=GMCR[-2]) assertEq(offset_neg1, base=GMCR[-1] + relativedelta(days=+1), expected=GMCR[-1]) assertEq(offset_neg2, base=GMCR[-1], expected=GMCR[-3]) - + date = GMCR[0] + relativedelta(days=-1) for expected in GMCR: assertEq(offset, date, expected) date = date + offset - + date = GMCR[-1] + relativedelta(days=+1) for expected in reversed(GMCR): assertEq(offset_neg1, date, expected) date = date + offset_neg1 - + def test_onOffset(self): lomq_aug_sat_4 = makeFY5253LastOfMonthQuarter(1, startingMonth=8, weekday=WeekDay.SAT, qtr_with_extra_week=4) lomq_sep_sat_4 = makeFY5253LastOfMonthQuarter(1, startingMonth=9, weekday=WeekDay.SAT, qtr_with_extra_week=4) - + tests = [ #From Wikipedia (lomq_aug_sat_4, datetime(2006, 8, 26), True), @@ -1414,78 +1448,78 @@ def test_onOffset(self): (lomq_aug_sat_4, datetime(2010, 8, 28), True), (lomq_aug_sat_4, datetime(2011, 8, 27), True), (lomq_aug_sat_4, datetime(2019, 8, 31), True), - + (lomq_aug_sat_4, datetime(2006, 8, 27), False), (lomq_aug_sat_4, datetime(2007, 8, 28), False), (lomq_aug_sat_4, datetime(2008, 8, 31), False), (lomq_aug_sat_4, datetime(2009, 8, 30), False), (lomq_aug_sat_4, datetime(2010, 8, 29), False), (lomq_aug_sat_4, datetime(2011, 8, 28), False), - + (lomq_aug_sat_4, datetime(2006, 8, 25), False), (lomq_aug_sat_4, datetime(2007, 8, 24), False), (lomq_aug_sat_4, datetime(2008, 8, 29), False), (lomq_aug_sat_4, datetime(2009, 8, 28), False), (lomq_aug_sat_4, datetime(2010, 8, 27), False), (lomq_aug_sat_4, datetime(2011, 8, 26), False), - (lomq_aug_sat_4, datetime(2019, 8, 30), False), + (lomq_aug_sat_4, datetime(2019, 8, 30), False), #From GMCR - (lomq_sep_sat_4, datetime(2010, 9, 25), True), - (lomq_sep_sat_4, datetime(2011, 9, 24), True), - (lomq_sep_sat_4, datetime(2012, 9, 29), True), - - (lomq_sep_sat_4, datetime(2013, 6, 29), True), - (lomq_sep_sat_4, datetime(2012, 6, 23), True), - (lomq_sep_sat_4, datetime(2012, 6, 30), False), - - (lomq_sep_sat_4, datetime(2013, 3, 30), True), - (lomq_sep_sat_4, datetime(2012, 3, 24), True), - - (lomq_sep_sat_4, datetime(2012, 12, 29), True), - (lomq_sep_sat_4, datetime(2011, 12, 24), True), - + (lomq_sep_sat_4, datetime(2010, 9, 25), True), + (lomq_sep_sat_4, datetime(2011, 9, 24), True), + (lomq_sep_sat_4, datetime(2012, 9, 29), True), + + (lomq_sep_sat_4, datetime(2013, 6, 29), True), + (lomq_sep_sat_4, datetime(2012, 6, 23), True), + (lomq_sep_sat_4, datetime(2012, 6, 30), False), + + (lomq_sep_sat_4, datetime(2013, 3, 30), True), + (lomq_sep_sat_4, datetime(2012, 3, 24), True), + + (lomq_sep_sat_4, datetime(2012, 12, 29), True), + (lomq_sep_sat_4, datetime(2011, 12, 24), True), + #INTC (extra week in Q1) #See: http://www.intc.com/releasedetail.cfm?ReleaseID=542844 (makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1), datetime(2011, 4, 2), True), - + #see: http://google.brand.edgar-online.com/?sym=INTC&formtypeID=7 (makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1), datetime(2012, 12, 29), True), (makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1), datetime(2011, 12, 31), True), (makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1), datetime(2010, 12, 25), True), - + ] for offset, date, expected in tests: assertOnOffset(offset, date, expected) - + def test_year_has_extra_week(self): #End of long Q1 self.assertTrue(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(2011, 4, 2))) - + #Start of long Q1 self.assertTrue(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(2010, 12, 26))) - + #End of year before year with long Q1 self.assertFalse(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(2010, 12, 25))) - - for year in [x for x in range(1994, 2011+1) if x not in [2011, 2005, 2000, 1994]]: + + for year in [x for x in range(1994, 2011+1) if x not in [2011, 2005, 2000, 1994]]: self.assertFalse(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(year, 4, 2))) - + #Other long years self.assertTrue(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(2005, 4, 2))) self.assertTrue(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(2000, 4, 2))) self.assertTrue(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).year_has_extra_week(datetime(1994, 4, 2))) - + def test_get_weeks(self): self.assertEqual(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).get_weeks(datetime(2011, 4, 2)), [14, 13, 13, 13]) self.assertEqual(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=4).get_weeks(datetime(2011, 4, 2)), [13, 13, 13, 14]) self.assertEqual(makeFY5253LastOfMonthQuarter(1, startingMonth=12, weekday=WeekDay.SAT, qtr_with_extra_week=1).get_weeks(datetime(2010, 12, 25)), [13, 13, 13, 13]) -class TestFY5253NearestEndMonthQuarter(unittest.TestCase): - +class TestFY5253NearestEndMonthQuarter(TestBase): + def test_onOffset(self): - + offset_nem_sat_aug_4 = makeFY5253NearestEndMonthQuarter(1, startingMonth=8, weekday=WeekDay.SAT, qtr_with_extra_week=4) offset_nem_thu_aug_4 = makeFY5253NearestEndMonthQuarter(1, startingMonth=8, weekday=WeekDay.THU, qtr_with_extra_week=4) tests = [ @@ -1496,19 +1530,19 @@ def test_onOffset(self): (offset_nem_sat_aug_4, datetime(2009, 8, 29), True), (offset_nem_sat_aug_4, datetime(2010, 8, 28), True), (offset_nem_sat_aug_4, datetime(2011, 9, 3), True), - + (offset_nem_sat_aug_4, datetime(2016, 9, 3), True), (offset_nem_sat_aug_4, datetime(2017, 9, 2), True), - (offset_nem_sat_aug_4, datetime(2018, 9, 1), True), + (offset_nem_sat_aug_4, datetime(2018, 9, 1), True), (offset_nem_sat_aug_4, datetime(2019, 8, 31), True), - + (offset_nem_sat_aug_4, datetime(2006, 8, 27), False), (offset_nem_sat_aug_4, datetime(2007, 8, 28), False), (offset_nem_sat_aug_4, datetime(2008, 8, 31), False), (offset_nem_sat_aug_4, datetime(2009, 8, 30), False), (offset_nem_sat_aug_4, datetime(2010, 8, 29), False), (offset_nem_sat_aug_4, datetime(2011, 8, 28), False), - + (offset_nem_sat_aug_4, datetime(2006, 8, 25), False), (offset_nem_sat_aug_4, datetime(2007, 8, 24), False), (offset_nem_sat_aug_4, datetime(2008, 8, 29), False), @@ -1516,11 +1550,11 @@ def test_onOffset(self): (offset_nem_sat_aug_4, datetime(2010, 8, 27), False), (offset_nem_sat_aug_4, datetime(2011, 8, 26), False), (offset_nem_sat_aug_4, datetime(2019, 8, 30), False), - + #From Micron, see: http://google.brand.edgar-online.com/?sym=MU&formtypeID=7 (offset_nem_thu_aug_4, datetime(2012, 8, 30), True), (offset_nem_thu_aug_4, datetime(2011, 9, 1), True), - + #See: http://google.brand.edgar-online.com/?sym=MU&formtypeID=13 (offset_nem_thu_aug_4, datetime(2013, 5, 30), True), (offset_nem_thu_aug_4, datetime(2013, 2, 28), True), @@ -1528,7 +1562,7 @@ def test_onOffset(self): (offset_nem_thu_aug_4, datetime(2012, 5, 31), True), (offset_nem_thu_aug_4, datetime(2007, 3, 1), True), (offset_nem_thu_aug_4, datetime(1994, 3, 3), True), - + ] for offset, date, expected in tests: @@ -1536,18 +1570,19 @@ def test_onOffset(self): def test_offset(self): offset = makeFY5253NearestEndMonthQuarter(1, startingMonth=8, weekday=WeekDay.THU, qtr_with_extra_week=4) - + MU = [datetime(2012, 5, 31), datetime(2012, 8, 30), datetime(2012, 11, 29), datetime(2013, 2, 28), datetime(2013, 5, 30)] - + date = MU[0] + relativedelta(days=-1) for expected in MU: assertEq(offset, date, expected) date = date + offset - + assertEq(offset, datetime(2012, 5, 31), datetime(2012, 8, 30)) assertEq(offset, datetime(2012, 5, 30), datetime(2012, 5, 31)) - -class TestQuarterBegin(unittest.TestCase): + +class TestQuarterBegin(TestBase): + def test_repr(self): self.assertEqual(repr(QuarterBegin()), "") self.assertEqual(repr(QuarterBegin(startingMonth=3)), "") @@ -1621,7 +1656,9 @@ def test_offset(self): self.assertEqual(datetime(2010, 2, 1) + offset, datetime(2010, 1, 1)) -class TestQuarterEnd(unittest.TestCase): +class TestQuarterEnd(TestBase): + _offset = QuarterEnd + def test_repr(self): self.assertEqual(repr(QuarterEnd()), "") self.assertEqual(repr(QuarterEnd(startingMonth=3)), "") @@ -1757,7 +1794,8 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestBYearBegin(unittest.TestCase): +class TestBYearBegin(TestBase): + _offset = BYearBegin def test_misspecified(self): self.assertRaises(ValueError, BYearBegin, month=13) @@ -1804,7 +1842,8 @@ def test_offset(self): assertEq(offset, base, expected) -class TestYearBegin(unittest.TestCase): +class TestYearBegin(TestBase): + _offset = YearBegin def test_misspecified(self): self.assertRaises(ValueError, YearBegin, month=13) @@ -1876,7 +1915,7 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestBYearEndLagged(unittest.TestCase): +class TestBYearEndLagged(TestBase): def test_bad_month_fail(self): self.assertRaises(Exception, BYearEnd, month=13) @@ -1917,7 +1956,8 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestBYearEnd(unittest.TestCase): +class TestBYearEnd(TestBase): + _offset = BYearEnd def test_offset(self): tests = [] @@ -1965,7 +2005,8 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestYearEnd(unittest.TestCase): +class TestYearEnd(TestBase): + _offset = YearEnd def test_misspecified(self): self.assertRaises(ValueError, YearEnd, month=13) @@ -2016,7 +2057,7 @@ def test_onOffset(self): assertOnOffset(offset, date, expected) -class TestYearEndDiffMonth(unittest.TestCase): +class TestYearEndDiffMonth(TestBase): def test_offset(self): tests = [] @@ -2193,10 +2234,10 @@ def test_compare_ticks(): assert(kls(3) != kls(4)) -class TestOffsetNames(unittest.TestCase): +class TestOffsetNames(unittest.TestCase): def test_get_offset_name(self): assertRaisesRegexp(ValueError, 'Bad rule.*BusinessDays', get_offset_name, BDay(2)) - + assert get_offset_name(BDay()) == 'B' assert get_offset_name(BMonthEnd()) == 'BM' assert get_offset_name(Week(weekday=0)) == 'W-MON' @@ -2229,7 +2270,7 @@ def test_get_offset(): offset = get_offset(name) assert offset == expected, ("Expected %r to yield %r (actual: %r)" % (name, expected, offset)) - + def test_parse_time_string(): (date, parsed, reso) = parse_time_string('4Q1984') @@ -2337,9 +2378,9 @@ def get_all_subclasses(cls): class TestCaching(unittest.TestCase): no_simple_ctr = [WeekOfMonth, FY5253, - FY5253Quarter, + FY5253Quarter, LastWeekOfMonth] - + def test_should_cache_month_end(self): self.assertTrue(MonthEnd()._should_cache())