def test_frame_constructor_too_big_range(self): try: excpt = (RuntimeError, pd._libs.tslibs.np_datetime.OutOfBoundsDatetime) except AttributeError: excpt = RuntimeError with pytest.raises(excpt): _Frame(start='21 Sep 1677', end='2017', base_unit_freq='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_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_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_organize_compound_when_strings_are_labels(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='10 Jan 2017') org = Organizer(marker='W', structure=['abc', 'x']) t = _Timeline(frame=f, organizer=org) assert t.labels.eq(['abc', 'x', 'abc']).all() assert t._frameband.eq([0,0,2,2,2,2,2,2,2,9,9]).all() assert (t._wsband.index == [0, 2, 9]).all()
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_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_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_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_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_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_amendments_empty(self): t = _Timeline(frame=_Frame(base_unit_freq='D', start='01 Jan 2017', end='10 Jan 2017'), data=0) amendments = {} t.amend(amendments) assert t.labels.eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).all()
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_amendments_all_outside(self): t = _Timeline(frame=_Frame(base_unit_freq='D', start='01 Jan 2017', end='10 Jan 2017'), data=0) amendments = {'02 Jan 2016': 1, '11 Jan 2017': 2} t.amend(amendments) assert t.labels.eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).all()
def test_organize_recursive_mixed(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017', end='10 Jan 2017') org_int = Organizer(marker='W', structure=[[1, 2, 3]]) org_ext = Organizer(marks=['06 Jan 2017'], structure=[org_int, [11, 12]]) t = _Timeline(frame=f, organizer=org_ext) assert t.labels.eq([1, 1, 2, 3, 1, 11, 12, 11, 12, 11]).all()
def test_organize_recursive_with_memory(self): f = _Frame(base_unit_freq='D', start='29 Nov 2017', end='06 Dec 2017') p = RememberingPattern(['a', 'b', 'c', 'd']) org_int = Organizer(marker='W', structure=[[1, 2], p]) org_ext = Organizer(marker='M', structure=[p, org_int]) t = _Timeline(frame=f, organizer=org_ext) assert t.labels.eq(['a', 'b', 1, 2, 1, 'c', 'd', 'a']).all()
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_organize_compound_alternate(self): f = _Frame(base_unit_freq='D', start='31 Dec 2016', end='10 Jan 2017') org = Organizer(marker='W', structure=[100, [1, 2, 3]]) t = _Timeline(frame=f, organizer=org) assert t.labels.eq([100, 1, 2, 3, 1, 2, 3, 1, 100]).all() assert t._frameband.eq([0,0,2,3,4,5,6,7,8,9,9]).all() assert (t._wsband.index == [0, 2,3,4,5,6,7,8, 9]).all()
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_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_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_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_amendments_subperiods(self): t = _Timeline(frame=_Frame(base_unit_freq='D', start='01 Jan 2017', end='10 Jan 2017'), data=0) amendments = {pd.Period('02 Jan 2017 12:00', freq='H'): 1, pd.Period('09 Jan 2017 15:00', freq='H'): 2} t.amend(amendments) assert t.labels.eq([0, 1, 0, 0, 0, 0, 0, 0, 2, 0]).all()
def test_amendments_timestamps(self): t = _Timeline(frame=_Frame(base_unit_freq='D', start='01 Jan 2017', end='10 Jan 2017'), data=0) amendments = {pd.Timestamp('02 Jan 2017'): 1, pd.Timestamp('09 Jan 2017'): 2} t.amend(amendments) assert t.labels.eq([0, 1, 0, 0, 0, 0, 0, 0, 2, 0]).all()
def test_frame_constructor_same_base_unit(self): f = _Frame(base_unit_freq='D', start='01 Jan 2017 08:00', end='01 Jan 2017 09:00') assert len(f)==1 assert f[0] == pd.Period('01 Jan 2017', freq='D') assert f.start_time == pd.Timestamp('01 Jan 2017 00:00:00') assert f.end_time >= pd.Timestamp('01 Jan 2017 23:59:59.999999999') assert f.end_time <= pd.Timestamp('02 Jan 2017 00:00:00') assert f.is_monotonic
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_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_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_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)