-1

i tried to plot a trend line on a graph using numpy but i got this error

ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('float64')

import pandas as pd
df = pd.read_excel (r'C:\Users\hussa\Downloads\Bo Karar.xlsx',"Data2")
print(df.head())

                    DataA      DataB
0 2022-06-21 02:54:00.761  24.377205
1 2022-06-21 02:54:10.761  24.377205
2 2022-06-21 02:54:20.762  24.377205
3 2022-06-21 02:54:30.762  24.377205
4 2022-06-21 02:54:40.762  24.377205

x=df['DataA']
y=df['DataB']

import matplotlib.pyplot as plt
import numpy as num
%matplotlib inline

fig=plt.figure()
plt.plot(x,y)
z = num.polyfit(x,y, 1)
p = num.poly1d(z)
plt.plot(x, p(x))

1 Answers1

1

I don't think you can use your date format for the polyfitting function, it expects a number (int, float) and the date is of format <M8[ns].

Convert your date values to a float time, i.e. seconds/ns since t0.

Loydms
  • 156
  • 3