ValueError: Wrong number of items passed 500, placement implies 1, Python and Pandas

25,198

ma and std are pandas.Series objects in your example. The reason is, that np.mean applied to a pandas.DataFrame returns a pandas.Series. However, mlab.normpdf(x,ma,std) expects float values or numpy arrays as inputs. You could simply convert ma and std to floats by ma = float(ma). I would not suggest to use int(ma) as you pointed out in your comment, because that would cut away the decimals.

Share:
25,198
DavidV
Author by

DavidV

Updated on November 13, 2020

Comments

  • DavidV
    DavidV over 3 years

    I'm importing just two columns from .xlsx file and I would like to calculate some stuff (mean, deviation, percent change) and then I would like to plot all this. First part doesn't give me any problems, but plotting does.

    My code looks like this:

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import matplotlib.mlab as mlab
    import math
    
    df = pd.read_excel('KDPrviIzbor.xlsx', sheetname='List1', index_col = 0)
    ch = df.pct_change(periods=252)
    
    ma = np.mean(ch)*100
    std = np.std(ch)*100
    
    x = np.linspace(-100,100,500)
    plt.plot(x,mlab.normpdf(x,ma,std))
    
    plt.show()
    

    But when I run my code, I get this error:

    Traceback (most recent call last):
    File "C:/Users/David/PythonStuff/normal_distribution.py", line 21, in <module> plt.plot(x,mlab.normpdf(x,ma,std))
    File "C:\Python27\lib\site-packages\matplotlib\mlab.py", line 1579, in normpdf return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-0.5 * (1./sigma*(x - mu))**2)
    File "C:\Python27\lib\site-packages\pandas\core\ops.py", line 534, in wrapper dtype=dtype)
    File "C:\Python27\lib\site-packages\pandas\core\series.py", line 220, in __init__ data = SingleBlockManager(data, index, fastpath=True)
    File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 3383, in __init__ ndim=1, fastpath=True)
    File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 2101, in make_block placement=placement)
    File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 77, in __init__ len(self.values), len(self.mgr_locs)))
    ValueError: Wrong number of items passed 500, placement implies 1`
    

    I figured that the problem is in:

    plt.plot(x,mlab.normpdf(x,ma,std))

    but I cannot solve it. Any suggestions?

  • DavidV
    DavidV over 8 years
    Fabian, thanks again. I used float(ma) and float(std) and now works as it should.