def test_days_splitby_weekly_atpoints_duplicates(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': 2, 'hours': 4 }, { 'days': 5 }, { 'days': 4, 'hours': 25 }])) # 'at' contains points referring to the same days # The result will be the same as with at=[{'days': 2}, {'days': 5}] 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_dangling_span(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='18 Jan 2017') result = f.partition_with_marker(_Span(4, 15), Marker('W')) assert len(result) == 3 assert assert_span(result[0], 4, 7, 3, 0) assert assert_span(result[1], 8, 14, 0, 0) assert assert_span(result[2], 15, 15, 0, 6)
def test_splitby_different_multiple_freqs7b(self): f = _Frame(base_unit_freq='7H', start='03 Jan 2017 00:00', end='30 Jan 2017') # these 7H periods are not aligned with boundaries of weeks with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('W'))
def test_days_splitby_annually_at_month_weekdays_shift(self): f = _Frame(base_unit_freq='D', start='01 Jan 2015', end='31 Dec 2017') # mark Wednesday after last Monday in May and Thursday before # first Monday in September # 25.05.15+2, 07.09.15-4, 30.05.16+2, 05.09.16-4, 29.05.17+2, 04.09.17-4 result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 5, 'week': -1, 'weekday': 1, 'shift': 2 }, { 'month': 9, 'week': 1, 'weekday': 1, 'shift': -4 }], how='nth_weekday_of_month')) assert len(result) == 7 assert assert_span(result[0], 0, 145, 126, 0) assert assert_span(result[1], 146, 244, 0, 0) assert assert_span(result[2], 245, 516, 0, 0) assert assert_span(result[3], 517, 608, 0, 0) assert assert_span(result[4], 609, 880, 0, 0) assert assert_span(result[5], 881, 972, 0, 0) assert assert_span(result[6], 973, 1095, 0, 149)
def test_4D_splitby_8D(self): f = _Frame(base_unit_freq='4D', start='01 Jan 2017', end='19 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('8D')) assert len(result) == 3 assert assert_span(result[0], 0, 1, 0, 0) assert assert_span(result[1], 2, 3, 0, 0) assert assert_span(result[2], 4, 4, 0, 1)
def test_days_splitby_annually_at_easter_orthodox(self): f = _Frame(base_unit_freq='D', start='31 Dec 2013', end='01 Jan 2017') # mark Good Friday, Easter and Easter Monday in 2014, 2015, 2016 result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'days': -2 }, { 'days': 0 }, { 'days': 1 }], how='from_easter_orthodox')) result_dates = [(f[x.first], f[x.last]) for x in result] assert len(result) == 10 assert result_dates[1][0] == Period('2014-04-18', 'D') assert result_dates[2][0] == Period('2014-04-20', 'D') #easter assert result_dates[3][0] == Period('2014-04-21', 'D') assert result_dates[4][0] == Period('2015-04-10', 'D') assert result_dates[5][0] == Period('2015-04-12', 'D') #easter assert result_dates[6][0] == Period('2015-04-13', 'D') assert result_dates[7][0] == Period('2016-04-29', 'D') assert result_dates[8][0] == Period('2016-05-01', 'D') #easter assert result_dates[9][0] == Period('2016-05-02', 'D')
def test_splitby_same_freq(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='03 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('D')) assert len(result) == 3 assert assert_span(result[0], 0, 0, 0, 0) assert assert_span(result[1], 1, 1, 0, 0) assert assert_span(result[2], 2, 2, 0, 0)
def test_splitby_different_multiple_freqs1(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='30 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('2W')) assert len(result) == 3 assert assert_span(result[0], 0, 8, 5, 0) assert assert_span(result[1], 9, 22, 0, 0) assert assert_span(result[2], 23, 30, 0, 6)
def test_days_splitby_weekly_atpoints_empty(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=[])) assert len(result) == 2 assert assert_span(result[0], 0, 6, 0, 0) assert assert_span(result[1], 7, 13, 0, 0)
def test_days_splitby_weekly_shifted(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='15 Jan 2017') result = f.partition_with_marker(_Span(1, 12), Marker('W-MON')) assert len(result) == 3 assert assert_span(result[0], 1, 1, 6, 0) assert assert_span(result[1], 2, 8, 0, 0) assert assert_span(result[2], 9, 12, 0, 3)
def test_days_splitby_annually_at_month_weekdays(self): f = _Frame(base_unit_freq='D', start='01 Jan 2015', end='31 Dec 2017') # mark last Monday in May and first Monday in September # 25.05.15, 07.09.15, 30.05.16, 05.09.16, 29.05.17, 04.09.17 result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 5, 'week': -1, 'weekday': 1 }, { 'month': 9, 'week': 1, 'weekday': 1 }], how='nth_weekday_of_month')) assert len(result) == 7 assert assert_span(result[0], 0, 143, 122, 0) assert assert_span(result[1], 144, 248, 0, 0) assert assert_span(result[2], 249, 514, 0, 0) assert assert_span(result[3], 515, 612, 0, 0) assert assert_span(result[4], 613, 878, 0, 0) assert assert_span(result[5], 879, 976, 0, 0) assert assert_span(result[6], 977, 1095, 0, 147)
def test_weeks_splitby_multiple_weeks_dangling(self): f = _Frame(base_unit_freq='W', start='02 Jan 2017', end='30 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('2W')) assert len(result) == 3 assert assert_span(result[0], 0, 1, 0, 0) assert assert_span(result[1], 2, 3, 0, 0) assert assert_span(result[2], 4, 4, 0, 1)
def test_days_splitby_monthly_at_weekdays(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') # set mark on second Monday and last Thursday # 09.01.17, 26.01.17, 13.02.17, 23.02.17 # left dangle from 29.12 == 2; right dangle thru 12.03 == 11 result = f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='M', at=[{ 'month': 1, 'week': 2, 'weekday': 1 }, { 'month': 1, 'week': -1, 'weekday': 4 }], how='nth_weekday_of_month')) assert len(result) == 5 assert assert_span(result[0], 0, 8, 2, 0) assert assert_span(result[1], 9, 25, 0, 0) assert assert_span(result[2], 26, 43, 0, 0) assert assert_span(result[3], 44, 53, 0, 0) assert assert_span(result[4], 54, 60, 0, 11)
def test_splitby_different_multiple_freqs2b(self): f = _Frame(base_unit_freq='12H', start='02 Jan 2017 13:05', end='04 Jan 2017 10:00') # these 12H periods are not aligned with boundaries of days with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('D'))
def test_days_splitby_monthly_dangling(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Feb 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('M')) assert len(result) == 3 assert assert_span(result[0], 0, 0, 30, 0) assert assert_span(result[1], 1, 31, 0, 0) assert assert_span(result[2], 32, 32, 0, 27)
def test_splitby_different_multiple_freqs3b(self): f = _Frame(base_unit_freq='12H', start='02 Jan 2017 01:00', end='30 Jan 2017') # these 12H periods are not aligned with boundaries of days, and, # consequently. of weeks with pytest.raises(UnacceptablePeriodError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('2W'))
def test_hours_splitby_weekly_dangling_span_short(self): f = _Frame(base_unit_freq='H', start='03 Jan 2017 01:00', end='15 Jan 2017 22:30') result = f.partition_with_marker(_Span(7 * 24 - 25 + 2, len(f) - 3), Marker('W')) assert len(result) == 1 assert assert_span(result[0], 7 * 24 - 25 + 2, len(f) - 3, 2, 3)
def test_hours_splitby_weekly_dangling(self): f = _Frame(base_unit_freq='H', start='03 Jan 2017 01:00', end='15 Jan 2017 22:30') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('W')) assert len(result) == 2 assert assert_span(result[0], 0, 7 * 24 - 25 - 1, 25, 0) assert assert_span(result[1], 7 * 24 - 25, 14 * 24 - 25 - 1 - 1, 0, 1)
def test_hours_splitby_weekly_aligned(self): f = _Frame(base_unit_freq='H', start='02 Jan 2017 00:00', end='15 Jan 2017 23:30') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('W')) assert len(result) == 2 assert assert_span(result[0], 0, 7 * 24 - 1, 0, 0) assert assert_span(result[1], 7 * 24, 14 * 24 - 1, 0, 0)
def test_days_splitby_weekly_dangling(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='18 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('W')) assert len(result) == 4 assert assert_span(result[0], 0, 0, 6, 0) assert assert_span(result[1], 1, 7, 0, 0) assert assert_span(result[2], 8, 14, 0, 0) assert assert_span(result[3], 15, 17, 0, 4)
def test_days_splitby_weekly_atpoints_badvalue(self): f = _Frame(base_unit_freq='D', start='02 Jan 2017', end='15 Jan 2017') with pytest.raises(TypeError): f.partition_with_marker(_Span(0, len(f) - 1), Marker('W', at=[{ 'days': 'nonsense' }]))
def test_splitby_different_multiple_freqs2(self): f = _Frame(base_unit_freq='12H', start='02 Jan 2017 12:05', end='04 Jan 2017 10:00') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('D')) assert len(result) == 3 assert assert_span(result[0], 0, 0, 1, 0) assert assert_span(result[1], 1, 2, 0, 0) assert assert_span(result[2], 3, 3, 0, 1)
def test_splitby_different_multiple_freqs3(self): f = _Frame(base_unit_freq='12H', start='02 Jan 2017', end='30 Jan 2017') result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('2W')) assert len(result) == 3 assert assert_span(result[0], 0, 27, 0, 0) assert assert_span(result[1], 28, 55, 0, 0) assert assert_span(result[2], 56, 57, 0, 26)
def test_days_splitby_weekly_atpoints_onlyzero(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 }])) # Here result will be the same as with at=None assert len(result) == 2 assert assert_span(result[0], 0, 6, 0, 0) assert assert_span(result[1], 7, 13, 0, 0)
def test_days_splitby_weekly_atpoints_float(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=[{ 'weeks': 0.29 }])) # 0.29 of a week is 2+ days, so the result is the same as with 'days':2 assert len(result) == 3 assert assert_span(result[0], 0, 1, 5, 0) assert assert_span(result[1], 2, 8, 0, 0) assert assert_span(result[2], 9, 13, 0, 2)
def test_days_splitby_monthly_at_day30(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') # set marks at 30th (=1+29) day of each month # no mark in February, there is just Jan 30 result = f.partition_with_marker(_Span(0, len(f) - 1), Marker('M', at=[{ 'days': 29 }])) assert len(result) == 2 assert assert_span(result[0], 0, 29, 1, 0) assert assert_span(result[1], 30, 60, 0, 28)
def test_days_splitby_weekly_atpoints_negative(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': -1 }])) # Negative offset falls out of a period. # Here `how` returns empty list and no partitioning is done, # dangles are undefined assert len(result) == 1 assert assert_span(result[0], 0, 13, -1, -1)
def test_days_splitby_weekly_atpoints_negative_compensated(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': -1, 'hours': 72 }])) # Negative day offset is compendates by positive hours assert len(result) == 3 assert assert_span(result[0], 0, 1, 5, 0) assert assert_span(result[1], 2, 8, 0, 0) assert assert_span(result[2], 9, 13, 0, 2)
def test_days_splitby_weekly_atpoints_excessive3(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': 7 }])) # Points which dall outside the period of marker freq # will be ignored # Here `how` returns empty list, hence no partitioning is done and # dangles are undefined assert len(result) == 1 assert assert_span(result[0], 0, 13, -1, -1)
def test_days_splitby_monthly_at_weekdays_bad_args(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='01 Mar 2017') for badmnth in [-1, 0, 13, 6.5, 'Jan']: with pytest.raises(AssertionError): f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': badmnth, 'week': 1, 'weekday': 7 }], how='nth_weekday_of_month')) for badweek in [-6, 0, 6, 3.5]: with pytest.raises(AssertionError): f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 1, 'week': badweek, 'weekday': 7 }], how='nth_weekday_of_month')) for badwday in [-1, 0, 8, 2.5, 'Mon']: with pytest.raises(AssertionError): f.partition_with_marker( _Span(0, len(f) - 1), Marker(each='A', at=[{ 'month': 1, 'week': 1, 'weekday': badwday }], how='nth_weekday_of_month'))