def test_days_splitby_monthly_at_weekdays_outside(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='M', at=[{ 'month': 2, 'week': 2, 'weekday': 1 }], how='nth_weekday_of_month')) # month=2 is outside the single month which is a period of `each` # therefore `how` returns empty list and no partitioning is done, # dangles are undefined assert len(result) == 1 assert assert_span(result[0], 0, 60, -1, -1)
def test_days_splitby_monthly_at_weekdays_gaps2(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='M', at=[{ 'month': 1, 'week': 5, 'weekday': 2 }], how='nth_weekday_of_month')) # 5th Tuesday is only in January (31 Jan) # Previous 5th Tuesday was in November 2016 and next will be # So we can provide skip_left but cannot calculate skip_right assert len(result) == 2 assert assert_span(result[0], 0, 30, 32, 0) assert assert_span(result[1], 31, 60, 0, -1)
def test_days_splitby_weekly_atpoints_2(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='17 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 2 }, { 'days': 5 }])) # Frame starts on Sat, ends on Tue # Subframes will be Wed to Fri, Sat to Tue -> No dangle # Here would be dangles with simple marker 'W' assert len(result) == 5 assert assert_span(result[0], 0, 3, 0, 0) # Sat 31 to Tue 3 assert assert_span(result[1], 4, 6, 0, 0) # Wed 4 to Fri 6 assert assert_span(result[2], 7, 10, 0, 0) # Sat 7 to Tue 10 assert assert_span(result[3], 11, 13, 0, 0) # Wed 11 to Fri 13 assert assert_span(result[4], 14, 17, 0, 0) # Sat 14 to Tue 17
def test_days_splitby_monthly_at_weekdays_gaps(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='01 Mar 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='M', at=[{ 'month': 1, 'week': 5, 'weekday': 2 }], how='nth_weekday_of_month')) # 5th Tuesday is only in January (31 Jan) # Previous 5th Tuesday was in November 2016 and next will be # only in May 2017 - both fell outside the envelope # So we cannot provide correct calculations of skip_left and skip_right assert len(result) == 2 assert assert_span(result[0], 0, 29, -1, 0) assert assert_span(result[1], 30, 59, 0, -1)
def test_days_splitby_weekly_atpoints_1(self): f = _Frame(base_unit_freq='D', start='02 Jan 2017', end='15 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 2 }, { 'days': 5 }])) # Frame starts on Mon, ends on Sun # Subframes will be Wed to Fri, Sat to Tue # Here would be no dangle with simple marker 'W' assert len(result) == 5 assert assert_span(result[0], 0, 1, 2, 0) # Mon 2 to Tue 3 # dangle from Sat 31 Dec assert assert_span(result[1], 2, 4, 0, 0) # Wed 4 to Fri 6 assert assert_span(result[2], 5, 8, 0, 0) # Sat 7 to Tue 10 assert assert_span(result[3], 9, 11, 0, 0) # Wed 11 to Fri 13 assert assert_span(result[4], 12, 13, 0, 2) # Sat 14 to Sun 15
def test_days_splitby_weekly_atpoints_3(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='16 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 2 }, { 'days': 5 }])) # Frame starts on Sun, ends on Mon # Subframes will be Wed to Fri, Sat to Tue # With simple marker 'W' here would be dangles but with other values assert len(result) == 5 assert assert_span(result[0], 0, 2, 1, 0) # Sun 1 to Tue 3 # dangle from Sat Dec 31 assert assert_span(result[1], 3, 5, 0, 0) # Wed 4 to Fri 6 assert assert_span(result[2], 6, 9, 0, 0) # Sat 7 to Tue 10 assert assert_span(result[3], 10, 12, 0, 0) # Wed 11 to Fri 13 assert assert_span(result[4], 13, 15, 0, 1) # Sat 14 to Mon 16
def test_days_splitby_annually_at_specific_date(self): f = _Frame(base_unit_freq='D', start='01 Jan 2015', end='31 Dec 2017') # set mark on Apr 11 result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('A', at=[{ 'months': 3, 'days': 10 }])) assert len(result) == 4 assert assert_span(result[0], 0, 99, 365 - 100, 0) #01.01.15 - # 10.04.15 assert assert_span(result[1], 100, 100 + 365, 0, 0) #11.04.15-10.04.16 # 2016 leap year assert assert_span(result[2], 466, 466 + 364, 0, 0) #11.04.16=10.04.17 assert assert_span(result[3], 831, 365 + 366 + 365 - 1, 0, 100) # 11.04.17=31.12.17 assert f[result[0].last].start_time == get_timestamp('10 Apr 2015') assert f[result[1].last].start_time == get_timestamp('10 Apr 2016') assert f[result[2].last].start_time == get_timestamp('10 Apr 2017')
def test_days_splitby_weekly_atpoints_withzerobound(self): f = _Frame(base_unit_freq='D', start='02 Jan 2017', end='15 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 0 }, { 'days': 2 }, { 'days': 5 }])) # Subframes will be Mon to Tue, Wed to Fri, Sat to Sun - > no dangle assert len(result) == 6 assert assert_span(result[0], 0, 1, 0, 0) assert assert_span(result[1], 2, 4, 0, 0) assert assert_span(result[2], 5, 6, 0, 0) assert assert_span(result[3], 7, 8, 0, 0) assert assert_span(result[4], 9, 11, 0, 0) assert assert_span(result[5], 12, 13, 0, 0)
def test_days_splitby_weekly_atpoints_excessive1(self): f = _Frame(base_unit_freq='D', start='02 Jan 2017', end='15 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 2, 'hours': 10 }, { 'days': 5, 'hours': 15 }])) # Adding hours does not change the result assert len(result) == 5 assert assert_span(result[0], 0, 1, 2, 0) # Mon 2 to Tue 3 # dangle from Sat 31 Dec assert assert_span(result[1], 2, 4, 0, 0) # Wed 4 to Fri 6 assert assert_span(result[2], 5, 8, 0, 0) # Sat 7 to Tue 10 assert assert_span(result[3], 9, 11, 0, 0) # Wed 11 to Fri 13 assert assert_span(result[4], 12, 13, 0, 2) # Sat 14 to Sun 15
def test_days_splitby_monthly_at_weekdays_gaps3n(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 1, 'week': -5, 'weekday': 2 }], how='nth_weekday_of_month')) # Here we look for minus 5th Tue in each January. There is only one # January in the frame which by the way such Tuesday on Jan 3. # But what about dangles? Envelope is 2015-2018 # There was no -5th Tues in Jan of 2015 and 2016, so we do not have # left dangle. There is -5th Tues in Jan 2018, so we have the right # dangle. assert len(result) == 2 assert assert_span(result[0], 0, 2, -1, 0) assert assert_span(result[1], 3, 60, 0, 306)
def test_days_splitby_monthly_at_weekdays_gaps4n(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 1, 'week': -5, 'weekday': 7 }], how='nth_weekday_of_month')) # Here we look for -5th Sun in each January. There is only one January # in the frame which by the way has such Sunday on Jan 1. # But what about dangles? Envelope is 2015-2018 # There was -5th Sunday in 2016, so we have left dangle. # There is no -5th Sunday in Jan 2018, so we do not have the right # dangle. assert len(result) == 2 assert assert_span(result[0], 0, 0, 363, 0) assert assert_span(result[1], 1, 60, 0, -1)
def test_days_splitby_weekly_atpoints_excessive2(self): f = _Frame(base_unit_freq='D', start='02 Jan 2017', end='15 Jan 2017') result = f.partition_with_marker( _Span(0, len(f) - 1), Marker('W', at=[{ 'days': 2, 'hours': 24 }, { 'days': 5, 'hours': 25 }])) # However adding to many hours does move the mark to the next day # this is same as days:3 and days:6 # Subframes will be Thu to Sat, Sun to Wed assert len(result) == 5 assert assert_span(result[0], 0, 2, 1, 0) # Mon 2 to Wed 4 # dangle from Sun 1 assert assert_span(result[1], 3, 5, 0, 0) # Thu 5 to Sat 7 assert assert_span(result[2], 6, 9, 0, 0) # Sun 8 to Wed 11 assert assert_span(result[3], 10, 12, 0, 0) # Thu 12 to Sat 14 assert assert_span(result[4], 13, 13, 0, 3) # Sun 15
def test_frame_split_empty(self): split_points=[] f = frame_60d() result = list(f._locate_subspans(_Span(0, len(f) - 1), split_points)) assert len(result)==1 assert result[0] == (0,59)
def test_splitby_different_multiple_freqs7(self): f = _Frame(base_unit_freq='7H', start='02 Jan 2017', end='30 Jan 2017') # These 7H periods are not aligned with days, but aligned with weeks result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('W'))
def test_splitby_different_multiple_freqs6(self): f = _Frame(base_unit_freq='48H', start='02 Jan 2017', end='30 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('4D'))
def test_splitby_different_multiple_freqs5(self): f = _Frame(base_unit_freq='48H', start='02 Jan 2017', end='30 Jan 2017') with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('D'))
def test_weeks_splitby_multiple_weeks(self): f = _Frame(base_unit_freq='W', start='02 Jan 2017', end='29 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('2W')) assert len(result) == 2 assert assert_span(result[0], 0, 1, 0, 0) assert assert_span(result[1], 2, 3, 0, 0)
def test_4D_splitby_months(self): f = _Frame(base_unit_freq='4D', start='01 Jan 2017', end='19 Jan 2017') with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('M'))
def test_4D_splitby_8D_span_dangling(self): f = _Frame(base_unit_freq='4D', start='01 Jan 2017', end='19 Jan 2017') result = f.partition_with_marker(_Span(1, 3), Marker('8D')) assert len(result) == 2 assert assert_span(result[0], 1, 2, 0, 0) assert assert_span(result[1], 3, 3, 0, 1)
def test_days_splitby_weekly_short(self): f = _Frame(base_unit_freq='D', start='03 Jan 2017', end='06 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('W')) assert len(result) == 1 assert assert_span(result[0], 0, 3, 1, 2)
def test_frame_locsubf_span_reverse_(self): split_points = [pd.Timestamp('10 Feb 2017 12:12:12')] f = frame_60d() with pytest.raises(VoidIntervalError): f._locate_subspans(_Span(55, 5), split_points)
def test_frame_locsubf_span_single_at_zero(self): split_points = [] f = frame_60d() result = list(f._locate_subspans(_Span(0, 0), split_points)) assert len(result) == 1 assert (result[0] == (0, 0))
def test_frame_locsubf_at_first_bu_only(self): split_points=[pd.Timestamp('06 Jan 2017')] f = frame_60d() result = list(f._locate_subspans(_Span(5, 55), split_points)) assert len(result)==1 assert (result[0] == (5,55))
def test_days_splitby_weekly_dangling_span_aligned(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='18 Jan 2017') result = f.partition_with_marker(_Span(1, 14), Marker('W')) assert len(result) == 2 assert assert_span(result[0], 1, 7, 0, 0) assert assert_span(result[1], 8, 14, 0, 0)
def test_frame_locsubf_span_single_points_outside(self): split_points=[pd.Timestamp('07 Jan 2017')] f = frame_60d() result = list(f._locate_subspans(_Span(5, 5), split_points)) assert len(result)==1 assert (result[0] == (5,5))
def test_weeks_splitby_years(self): f = _Frame(base_unit_freq='W', start='31 Dec 2016', end='01 Mar 2017') with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('A'))
def test_frame_locsubf_span_outside(self): split_points = [pd.Timestamp('10 Feb 2017 12:12:12')] f = frame_60d() with pytest.raises(OutOfBoundsError): f._locate_subspans(_Span(5, 70), split_points)
def test_months_splitby_years_short(self): f = _Frame(base_unit_freq='M', start='01 Feb 2017', end='01 Mar 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('A')) assert len(result) == 1 assert assert_span(result[0], 0, 1, 1, 9)
def test_frame_locsubf_span_ts_injection2(self): split_points = [pd.Timestamp('10 Feb 2017 12:12:12')] f = frame_60d() with pytest.raises(Exception): f._locate_subspans(_Span(5, pd.Timestamp('20 Feb 2017')), split_points)
def test_months_splitby_years_shifted(self): f = _Frame(base_unit_freq='M', start='31 Dec 2016', end='01 Mar 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('A-JAN')) assert len(result) == 2 assert assert_span(result[0], 0, 1, 10, 0) assert assert_span(result[1], 2, 3, 0, 10)