Pandas DataFrame column assignment ValueError: Wrong number of items passed

删除回忆录丶 提交于 2019-12-07 21:50:34

问题


I am having an issue with a script that was functioning prior to an upgrade of Anaconda (thus an upgrade of pandas and 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.

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

ValueError: Wrong number of items passed 3509, placement implies 1.

self.positions has this appearance (its index is around 3600):

    Symbol
1    int
2    int
3    int

self.bars.Close has this apperance (same index size as 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.

Any help is greatly appreciated.


回答1:


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


来源:https://stackoverflow.com/questions/34620118/pandas-dataframe-column-assignment-valueerror-wrong-number-of-items-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!