def test_not_in_roll_one_generic_static_roller(dates): dt = dates.iloc[0] contract_dates = dates.iloc[0:2] sd, ed = (dt + BDay(-8), dt + BDay(-7)) timestamps = pd.date_range(sd, ed, freq='b') cols = pd.MultiIndex.from_product([[0], ['front', 'back']]) idx = [-2, -1, 0] trans = pd.DataFrame([[1.0, 0.0], [0.5, 0.5], [0.0, 1.0]], index=idx, columns=cols) midx = pd.MultiIndex.from_product([timestamps, ['CLX16']]) midx.names = ['date', 'contract'] cols = pd.Index([0], name='generic') wts_exp = pd.DataFrame([1.0, 1.0], index=midx, columns=cols) # with DatetimeIndex wts = mappings.roller(timestamps, contract_dates, mappings.static_transition, transition=trans) assert_frame_equal(wts, wts_exp) # with tuple wts = mappings.roller(tuple(timestamps), contract_dates, mappings.static_transition, transition=trans) assert_frame_equal(wts, wts_exp)
def test_roll_to_roll_two_generics(): contract_dates = pd.Series([ TS('2016-10-10'), TS('2016-10-13'), TS('2016-10-17'), TS('2016-10-20') ], index=['A', 'B', 'C', 'D']) timestamps = pd.date_range(contract_dates.iloc[0] + BDay(-2), contract_dates.iloc[1], freq='b') cols = pd.MultiIndex.from_product([[0, 1], ['front', 'back']]) idx = [-2, -1, 0] trans = pd.DataFrame([[1, 0, 1, 0], [0.5, 0.5, 0.5, 0.5], [0, 1, 0, 1]], index=idx, columns=cols) wts = mappings.roller(timestamps, contract_dates, mappings.static_transition, transition=trans) midx = pd.MultiIndex.from_tuples([ (timestamps[0], 'A'), (timestamps[0], 'B'), (timestamps[1], 'A'), (timestamps[1], 'B'), (timestamps[1], 'C'), (timestamps[2], 'B'), (timestamps[2], 'C'), (timestamps[3], 'B'), (timestamps[3], 'C'), (timestamps[4], 'B'), (timestamps[4], 'C'), (timestamps[4], 'D'), (timestamps[5], 'C'), (timestamps[5], 'D') ]) midx.names = ['date', 'contract'] cols = pd.Index([0, 1], name='generic') vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1], [1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]] wts_exp = pd.DataFrame(vals, index=midx, columns=cols) assert_frame_equal(wts, wts_exp)
def test_whole_roll_roll_two_generics_static_roller(dates): dt = dates.iloc[0] contract_dates = dates timestamps = pd.DatetimeIndex([dt + BDay(-2), dt + BDay(-1), dt]) cols = pd.MultiIndex.from_product([[0, 1], ['front', 'back']]) idx = [-2, -1, 0] trans = pd.DataFrame([[1, 0, 1, 0], [0.5, 0.5, 0.5, 0.5], [0, 1, 0, 1]], index=idx, columns=cols) wts = mappings.roller(timestamps, contract_dates, mappings.static_transition, transition=trans) midx = pd.MultiIndex.from_tuples([(timestamps[0], 'CLX16'), (timestamps[0], 'CLZ16'), (timestamps[1], 'CLF17'), (timestamps[1], 'CLX16'), (timestamps[1], 'CLZ16'), (timestamps[2], 'CLF17'), (timestamps[2], 'CLZ16')]) midx.names = ['date', 'contract'] cols = pd.Index([0, 1], name='generic') wts_exp = pd.DataFrame( [[1, 0], [0, 1], [0, 0.5], [0.5, 0], [0.5, 0.5], [0, 1], [1, 0]], index=midx, columns=cols) assert_frame_equal(wts, wts_exp)
def test_invalid_contract_dates(): ts = [pd.Timestamp("2016-10-19")] cols = pd.MultiIndex.from_product([[0, 1], ['front', 'back']]) idx = [-1, 0] trans = pd.DataFrame([[1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0]], index=idx, columns=cols) non_unique_index = pd.Series( [pd.Timestamp('2016-10-20'), pd.Timestamp('2016-11-21')], index=['instr1', 'instr1']) with pytest.raises(ValueError): mappings.roller(ts, non_unique_index, mappings.static_transition, transition=trans) non_unique_vals = pd.Series( [pd.Timestamp('2016-10-20'), pd.Timestamp('2016-10-20')], index=['instr1', 'instr2']) with pytest.raises(ValueError): mappings.roller(ts, non_unique_vals, mappings.static_transition, transition=trans) non_monotonic_vals = pd.Series( [pd.Timestamp('2016-10-20'), pd.Timestamp('2016-10-19')], index=['instr1', 'instr2']) with pytest.raises(ValueError): mappings.static_transition(ts[0], non_monotonic_vals, transition=trans) not_enough_vals = pd.Series([pd.Timestamp('2016-10-19')], index=['instr1']) with pytest.raises(IndexError): mappings.static_transition(ts[0], not_enough_vals, transition=trans)