动态附加到系列

时间:2015-08-13 16:30:00

标签: python pandas

我正在尝试在循环中附加到Series对象,如下面的代码所示,但发现附加根本没有发生。 bond.to_cash_flows().amounts会产生如下所示的系列,我只想尝试附加到此系列。

例如,系列之一看起来像:

     1983-05-15      1
     1983-11-15      1
     1984-05-15      1
     1984-11-15     101

list_of_dataindices_of_data正在按要求运作。而print(list_of_data) = [0 0]print(indices_of_data) = [datetime.date(2018, 5, 15), datetime.date(2018, 11, 15)]我需要上面的系列看起来像:

     1983-05-15      1
     1983-11-15      1
     1984-05-15      1
     1984-11-15     101
     2018-5-15       0
     2018-11-15      0

然而,print('SERIES', bond.to_cash_flows().amounts)导致与之前相同的系列,我不知道为什么会这样。

for bond in bonds:
    list_of_data = [0] * (length_of_series_max_maturity - len(bond.to_cash_flows().amounts))
    print(list_of_data)
    indices_of_data = list(set(indices_of_series_max_maturity)^set(bond.to_cash_flows().amounts.index))
    print(indices_of_data) 
    print(bond.to_cash_flows().amounts)
    series = pandas.Series(list_of_data, indices_of_data)
    bond.to_cash_flows().amounts = bond.to_cash_flows().amounts.append(series)
    print('SERIES', bond.to_cash_flows().amounts)

1 个答案:

答案 0 :(得分:0)

我认为问题在于

bond.to_cash_flows()

是方法调用,输出(及其属性)不能更改 以你完成的方式分配。就是这一行

bond.to_cash_flows().amounts = bond.to_cash_flows().amounts.append(series)

无效。你可以做点什么

amounts = bond.to_cash_flows().amounts.append(Series)
print('SERIES', amounts)

不确定这会解决问题,因为我不知道上下文但会在示例中打印出你想要的内容

相关问题