Pandas DataFrame column assignment ValueError: Wrong number of items passed

Grr picture Grr · Jan 5, 2016 · Viewed 8.1k times · Source

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.

Answer

Stefan picture Stefan · Jan 5, 2016

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