##### Linear Regression on Time Series
 Linear Regression on Time Series karlito Silly Frenchman Posts: 48 Threads: 17 Joined: Oct 2019 Reputation: Jan-27-2020, 02:19 PM (This post was last modified: Jan-27-2020, 02:21 PM by buran.) Hi, I'm trying this time to use a simple linear regression on my time series dataset to linearly predict data. But I got this error and I don't know how to handle it. Any ideas? ```# print df.head() eie Date_Time 2017-11-10 4470.76 2017-11-11 5465.72 2017-11-12 15465.72 2017-11-13 25465.72 2017-11-14 21480.59 y = np.array(df.values, dtype=float) x = np.array(pd.to_datetime(df['eie']).index.values, dtype=float) slope, intercept, r_value, p_value, std_err =sp.linregress(x,y) xf = np.linspace(min(x),max(x),100) xf1 = xf.copy() xf1 = pd.to_datetime(xf1) yf = (slope*xf)+intercept print('r = ', r_value, '\n', 'p = ', p_value, '\n', 's = ', std_err)```# Error ``````Error:ValueError Traceback (most recent call last) in 1 y = np.array(df.values, dtype=float) ----> 2 x = np.array(pd.to_datetime(df['eie']).index.values, dtype=float) 3 4 slope, intercept, r_value, p_value, std_err =sp.linregress(x,y) 5 ValueError: could not convert string to float: '2017-11-10'`````` Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jan-27-2020, 02:22 PM The error is clear - string `'2017-11-10'` could not be converted to float (obviously) If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply karlito Silly Frenchman Posts: 48 Threads: 17 Joined: Oct 2019 Reputation: Jan-27-2020, 02:34 PM (Jan-27-2020, 02:22 PM)buran Wrote: The error is clear - string `'2017-11-10'` could not be converted to float (obviously) Yes I can read :) but for regression purpose, I read that all dates should be passed through pandas 'to_datetime()' function to convert it to float numeric because corresponding dates will be saved in the 'x' variable. nb: before setting Date_Time as index it was already converted to 'to_datetime()'. I'm kind of lost. Any ideas? Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jan-27-2020, 03:00 PM (This post was last modified: Jan-27-2020, 03:00 PM by buran.) maybe ```import pandas as pd import numpy as np df = pd.DataFrame([['2017-11-10', 4470.76], ['2017-11-11', 5465.72], ['2017-11-12', 15465.72]], columns=['Date_Time', 'eie']) y = np.array(df['eie'], dtype=float) x = np.array(pd.to_datetime(df['Date_Time'], format='%Y-%m-%d'), dtype=float) print(y) print(x)`````````Output:[ 4470.76 5465.72 15465.72] [1.5102720e+18 1.5103584e+18 1.5104448e+18]`````` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply karlito Silly Frenchman Posts: 48 Threads: 17 Joined: Oct 2019 Reputation: Jan-28-2020, 09:20 AM (Jan-27-2020, 03:00 PM)buran Wrote: maybe ```import pandas as pd import numpy as np df = pd.DataFrame([['2017-11-10', 4470.76], ['2017-11-11', 5465.72], ['2017-11-12', 15465.72]], columns=['Date_Time', 'eie']) y = np.array(df['eie'], dtype=float) x = np.array(pd.to_datetime(df['Date_Time'], format='%Y-%m-%d'), dtype=float) print(y) print(x)`````````Output:[ 4470.76 5465.72 15465.72] [1.5102720e+18 1.5103584e+18 1.5104448e+18]`````` Thanks for your effort but it doesn't really helps me, sorry. I wish something like ``````Output:[ 4470.76 5465.72 15465.72] [1 2 3]`````` or even better ``````Output:[ 4470.76 5465.72 15465.72] [2017-11-10 2017-11-11 2017-11-12]`````` Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jan-28-2020, 10:02 AM (This post was last modified: Jan-28-2020, 10:03 AM by buran.) I think there is some confusion in your understanding, but anyway ```import pandas as pd import numpy as np df = pd.DataFrame([['2017-11-10', 4470.76], ['2017-11-11', 5465.72], ['2017-11-12', 15465.72]], columns=['Date_Time', 'eie']) y = np.array(df['eie'], dtype=float) x = np.array(pd.to_datetime(df['Date_Time'], format='%Y-%m-%d'), dtype='datetime64[D]') print(y) print(x)`````````Output:[ 4470.76 5465.72 15465.72] ['2017-11-10' '2017-11-11' '2017-11-12']``````or ```import pandas as pd import numpy as np df = pd.DataFrame([['2017-11-10', 4470.76], ['2017-11-11', 5465.72], ['2017-11-12', 15465.72]], columns=['Date_Time', 'eie']) y = np.array(df['eie'], dtype=float) x = np.array(pd.to_datetime(df['Date_Time'].index.values+1, format='%Y-%m-%d'), dtype=int) print(y) print(x)`````````Output:[ 4470.76 5465.72 15465.72] [1 2 3]`````` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Recommendations for ML libraries for time-series forecast AndreasPython 0 565 Jan-06-2021, 01:03 PM Last Post: AndreasPython Time Series forecating with multiple independent variables Krychol88 1 539 Oct-23-2020, 08:11 AM Last Post: DPaul how to handling time series data file with Python? aupres 4 1,070 Aug-10-2020, 12:40 PM Last Post: MattKahn13 Changing Time Series from Start to End of Month illmattic 0 738 Jul-16-2020, 10:49 AM Last Post: illmattic HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 683 Jul-11-2020, 09:04 PM Last Post: bntayfur Differencing Time series and Inverse after Training donnertrud 0 1,674 May-27-2020, 06:11 AM Last Post: donnertrud Linear regression doubt - Urgent kumarants 6 1,106 May-05-2020, 04:11 PM Last Post: kumarants How can I convert time-series data in rows into column srvmig 0 880 Apr-11-2020, 05:40 AM Last Post: srvmig Using shift to compute the percent change in a time series new_to_python 6 1,594 Mar-03-2020, 07:50 PM Last Post: new_to_python prediction using linear regression (extrapolation?) in a loop karlito 0 1,434 Feb-05-2020, 10:56 AM Last Post: karlito

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020