This is my data:
_time is on the x-axis and IdCount is y-axis. I converted _time first to datetime and then to float by using this code:
#to datetime
df['_time'] = pd.to_datetime(df['_time'])
#creating list of _time column
time = df['_time'].tolist()
#time to float in order to make np.polyfit work
def datetime_to_float(d):
return d.timestamp()
time_in_float = []
for i in time:
time_in_float.append(datetime_to_float(i))
After this I added time_in_float to my pandas dataframe:
df['time_float'] = time_in_float
and defined x- and y-variables:
x = df['time_float']
y = df['IdCount']
I used the following code to draw a trendline in my data:
plt.plot(x, y)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()
I now would like to get the equation of this trendline: y = ax + b
I used this code I found on StackOverflow:
print("{0}x + {1}".format(*z))
But the parameters I got do not make a lot of sense.
How should I modify my code?
Thanks!
Edit: The code here is okay, but with the solution provided it works better.
I suggest you to convert your dates to float (in seconds) this way :
df['_time'] = pd.to_datetime(df['_time'])
# compute timedelta from initial time
dt = df['_time'] - df['_time'][0]
# convert timedelta to seconds
# you can choose hours or days or other units here if you want
df['time_float']= dt.astype('timedelta64[s]')