1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to fix the error cannot convert the series to <class 'float'>

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I have a excel data .xslx, with 4 columns and many rows and I just want to extract the column 0 that is my wavelength and the 3 column that is my radiation, and I make a pyplots from that data. My archive looks like this

    280.0 8.2000E-02 4.7309E-23 2.5361E-26
    280.5 9.9000E-02 1.2307E-21 1.0917E-24
    281.0 1.5000E-01 5.6895E-21 6.1253E-24
    281.5 2.1200E-01 1.5662E-19 2.7479E-22
    282.0 2.6700E-01 1.1946E-18 2.8346E-21
    282.5 3.0300E-01 4.5436E-18 1.3271E-20


    Next I want to use curve_fit to adjust a black body to the peplos using Planck's equation. But when I use curve fit I got an error:

    File"/Applications/Spyder.app/Contents/Resources/lib/python3.9/pandas/core/series.py", line 191, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")

    TypeError: cannot convert the series to <class 'float'>


    This error comes when the program try to calculate the covariance. I tried to change my radiation nd wavelength using astype(float) but it doesn't work.

    import pandas as pd
    import math
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.constants import h, c, k
    from scipy.optimize import curve_fit
    Data=pd.read_excel('Sun_Data.xlsx', sheet_name='Spectra',header=2)
    #fname='Sun_Data.txt'
    #A=np.loadtxt(fname)

    wavelength=Data.iloc[:,0].astype(float)
    rad=Data.iloc[:,3].astype(float)

    #wave=list(wavelength)
    #print(wave)
    def funcion(wavelegth,T):
    planck=(2*math.pi*c**2*h)/(wavelength**5*(math.exp(h*c/(wavelength*k*T))-1))
    return planck

    #print(funcion(wavelength, rad))
    t,cov=curve_fit(funcion, wavelength, rad)
    plt.plot(wavelength,rad, color='pink')
    plt.show()


    I think my error is using pandas to read the data but I'm not sure, can someone help me.

    Continue reading...

Compartilhe esta Página