0
votes

Edited to include full code and after attempting one solution:

I'm getting an error: UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('float64') when following a tutorial to try and plot a trendline on a scatterplot I created, here's the code:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('dataSet.csv')

date = df['Date'].astype('datetime64')
actual = df['Actual'].astype('float64')

plt.figure(figsize=(20,10))
plt.scatter(date, actual, color='orange')

z = np.polyfit(date, actual, 1)
p = np.poly1d(z)

plt.plot(date, p(date), "r--")

plt.show()

if I replace the formula for z with:

z = np.polyfit(list(date), actual, 1)

it's a slightly different error, but still an error: TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'

1
Please include a toy dataset that reproduces the problem. For a start - I don't see where in this code you plot the fit line. - Mr. T
How do I add the csv data here to make it most useful? - user2981194
Upload it externally and post the link. Make sure it is publicly accessible. - Mr. T

1 Answers

0
votes

I just tried this method with your dataset, and it worked:

df["Date"] = pd.to_datetime(df["Date"])
plt.scatter(df["Date"]), df["Actual"])