Pandas DataFrame 列分配 ValueError:传递的项目数错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34620118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas DataFrame column assignment ValueError: Wrong number of items passed
提问by Grr
I am having an issue with a script that was functioning prior to an upgrade of Anaconda (thus an upgrade of pandas and numpy)
我在升级 Anaconda 之前运行的脚本有问题(因此升级了 pandas 和 numpy)
I have a DataFrame that I would like to use one column from and multiply by the values in a column of another DataFrame, outputting the final value to a column in a new DataFrame. As I said this code was working until I upgraded to pandas 0.17.
我有一个 DataFrame,我想使用其中的一列并乘以另一个 DataFrame 的一列中的值,将最终值输出到新 DataFrame 中的一列。正如我所说,这段代码一直有效,直到我升级到 Pandas 0.17。
class MarketOnClosePortfolio(Portfolio):
def __init__(self, symbol, bars, signals, initial_capital=10000.0):
self.symbol = symbol
self.bars = bars
self.signals = signals
self.initial_capital = float(initial_capital)
self.positions = self.generate_positions()
def generate_positions(self):
positions = pd.DataFrame(index=signals.index).fillna(0.0)
positions[self.symbol] = signals['signal']*10
return positions
def backtest_portfolio(self):
portfolio = self.positions*self.bars['Close']
pos_diff = self.positions.diff()
portfolio = pd.DataFrame(index=signals.index)
portfolio['holdings'] = (self.positions*self.bars['Close'])
portfolio['cash'] = self.initial_capital - (pos_diff*self.bars['Close']).sum(axis=1).cumsum()
portfolio['total'] = portfolio['cash'] + portfolio['holdings']
portfolio['returns'] = portfolio['total'].pct_change()
return portfolio
if __name__ == "__main__":
portfolio = MarketOnClosePortfolio(symbol, bars, signals, initial_capital=10000.0)
returns = portfolio.backtest_portfolio()
My error comes in when trying to execute returns = portfolio.backtest_portfolio()
referring back to portfolio['holdings'] = self.positions*self.bars['Close']
and returns
尝试执行returns = portfolio.backtest_portfolio()
引用portfolio['holdings'] = self.positions*self.bars['Close']
并返回时出现错误
ValueError: Wrong number of items passed 3509, placement implies 1.
ValueError: 错误的项目数通过 3509,放置意味着 1。
self.positions has this appearance (its index is around 3600):
self.positions 有这个样子(它的指数在 3600 左右):
Symbol
1 int
2 int
3 int
self.bars.Close has this apperance (same index size as self.positions):
self.bars.Close 具有以下外观(与 self.positions 的索引大小相同):
Close
1 float
2 float
3 float
Am i overlooking something obvious here? I know I am passing a series and not a single value, but i am confused why i am getting "placement implies 1" out.
我在这里忽略了一些明显的东西吗?我知道我传递的是一个系列而不是单个值,但我很困惑为什么我会得到“放置意味着 1”。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
回答by Stefan
Try adjusting your multiplication along the lines of the below:
尝试按照以下方法调整乘法:
position = pd.DataFrame({'symbol': [ 1,2,3,4,5]})
bar = pd.DataFrame({'close': np.random.random(5)})
position.symbol.mul(bar.close, axis=0)
0 0.184591
1 1.830434
2 0.343875
3 1.531412
4 2.257981
dtype: float64