Home > Software engineering >  How to add a trendline to a time series line chart without casting error?
How to add a trendline to a time series line chart without casting error?

Time:07-21

I'm trying to add a trendline to my output plot. My dataframe looks something likes this:

Years   Percentage increase
2021    1.480378
2022    1.270628
2023    0.42773
2024    2.271726
2025    3.857106
2026    2.123221
2027    2.658651
2028    2.786197
2029    2.106992
2030    2.959151

I'm trying to use the following code to get the trendline:

plt.plot(df['Years'], df['Percentage increase'])
x=df['Years']
y=df['Percentage increase']
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
pylab.plot(x,p(x),"r--")

But I'm getting the following error:

UFuncTypeError: Cannot cast ufunc 'lstsq_n' input 1 from dtype('O') to dtype('float64') with casting rule 'same_kind'

Can anyone help me out on how to resolve this issue?

CodePudding user response:

It looks like you have python objects stored in your DataFrame rather than floats, which numpy doesn't know how to handle. I can't say which elements in your DataFrame are objects, but you could either change how you constrct the DataFrame to ensure everything is a float, or cast elements to floats before the polyfit command. Your code should work fine if both df['Years'] and df['Percentage increase'] return lists of numbers

  • Related