Fitting a pandas dataframe to a Poisson Distribution I have a simple dataframe df2 that consist of indices and one column of values. I want to fit this dataframe to a poisson distribution. Below is the code I am using:

import numpy as np
from scipy.optimize import curve_fit    
data=df2.values
bins=df2.index
def poisson(k, lamb):
    return (lamb^k/ np.math.factorial(k)) * np.exp(-lamb)

params, cov =  curve_fit(poisson, np.array(bins.tolist()), data.flatten())


I get the following error:


  TypeError: only size-1 arrays can be converted to Python scalars
I think the cause of the error is the np.math.factorial(k) function call, since curve_fit passes a numpy array as the first parameter to the poisson function, and if you try to run the code

np.math.factorial(np.array([1, 2, 3]))


You'll get the error

TypeError: only size-1 arrays can be converted to Python scalars


Try using scipy.special.factorial since it accepts a numpy array as input instead of only accepting scalers.

Thus, just change your poisson function to 

def poisson(k, lamb):
    return (lamb**k/ scipy.special.factorial(k)) * np.exp(-lamb)


Hope this helps

EDIT:
Also I fixed the ^ to ** since that's how you use the exponential operator in python.