我的数据框看起来像这样:
time price
0 2019-02-01 00:07:00 0.00234135
1 2019-02-01 00:10:15 0.0023541
2 2019-02-01 00:13:30 0.00235838
3 2019-02-01 01:03:00 0.00236977
4 2019-02-01 01:07:00 0.00237751
我之后做的是使用以下代码计算MACD:macd, macd_signal, macd_histogram = ti.macd(data,10,20,9)
我现在想要创建一个包含相关macd值的新列:df['macd'] = pd.Series(macd)
但前20个值用于计算macd,因此前20个值没有macd值。
然后我应该创建一个从索引20开始的macd值的列。我试过:df.at[18, 'macd'] = pd.Series(macd)
但它不起作用我有以下错误消息:
ValueError: setting an array element with a sequence.
有帮助吗?谢谢!
使用未定义的索引转换为Series
并不是一个好主意,因为可能不会在新的Series
和旧索引之间进行对齐:
df.loc[18:, 'macd'] = macd[18:]
使用pd.Series
解决方案:
df.loc[18:, 'macd'] = pd.Series(macd, index=df.index)