title: How to get slope from timeseries data in pandas?
body: <p>I have a pandas dataframe which contains <code>date</code> and some values something like below</p>
  
  <p><strong>Original data:</strong></p>
  
  <pre><code>list = [('2018-10-29', 6.1925), ('2018-10-29', 6.195), ('2018-10-29', 1.95833333333333), 
          ('2018-10-29', 1.785), ('2018-10-29', 3.05), ('2018-10-29', 1.30666666666667), 
          ('2018-10-29', 1.6325), ('2018-10-30', 1.765), ('2018-10-30', 1.265), 
          ('2018-10-30', 2.1125), ('2018-10-30', 2.16714285714286), ('2018-10-30', 1.485), 
          ('2018-10-30', 1.72), ('2018-10-30', 2.754), ('2018-10-30', 1.79666666666667), 
          ('2018-10-30', 1.27833333333333), ('2018-10-30', 3.48), ('2018-10-30', 6.19), 
          ('2018-10-30', 6.235), ('2018-10-30', 6.11857142857143), ('2018-10-30', 6.088), 
          ('2018-10-30', 4.3), ('2018-10-30', 7.80666666666667), 
          ('2018-10-30', 7.78333333333333), ('2018-10-30', 10.9766666666667), 
          ('2018-10-30', 2.19), ('2018-10-30', 1.88)]
  </code></pre>
  
  <p><strong>After loading into pandas</strong></p>
  
  <pre><code>df = pd.DataFrame(list)
  
  
               0          1
  0   2018-10-29   6.192500
  1   2018-10-29   6.195000
  2   2018-10-29   1.958333
  3   2018-10-29   1.785000
  4   2018-10-29   3.050000
  5   2018-10-29   1.306667
  6   2018-10-29   1.632500
  7   2018-10-30   1.765000
  8   2018-10-30   1.265000
  9   2018-10-30   2.112500
  10  2018-10-30   2.167143
  11  2018-10-30   1.485000
  12  2018-10-30   1.720000
  13  2018-10-30   2.754000
  14  2018-10-30   1.796667
  15  2018-10-30   1.278333
  16  2018-10-30   3.480000
  17  2018-10-30   6.190000
  18  2018-10-30   6.235000
  19  2018-10-30   6.118571
  20  2018-10-30   6.088000
  21  2018-10-30   4.300000
  22  2018-10-30   7.806667
  23  2018-10-30   7.783333
  24  2018-10-30  10.976667
  25  2018-10-30   2.190000
  26  2018-10-30   1.880000
  </code></pre>
  
  <p>This is how I load up the dataframe</p>
  
  <pre><code>df = pd.DataFrame(list)
  df[0] = pd.to_datetime(df[0], errors='coerce')
  df.set_index(0, inplace=True)
  </code></pre>
  
  <p>Now I want to find the <code>slope</code>. Upon research in the internet, I found this is what is needed to be done to get the <code>slope</code></p>
  
  <pre><code>trend_coord = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df.iloc[:,0].values).trend.interpolate(method='linear',axis=0).fillna(0).values)))
  
  results = sm.OLS(np.asarray(sm.tsa.seasonal_decompose(df.iloc[:,0].values).trend.interpolate(method='linear', axis=0).fillna(0).values), sm.add_constant(np.array([i for i in range(len(trend_coord))])), missing='drop').fit()
  
  slope = results.params[1]
  print(slope)
  </code></pre>
  
  <p>But I get the below error</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/souvik/Music/UI_Server2/test35.py", line 11, in &lt;module&gt;
      trend_coord = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df.iloc[:,0].values).trend.interpolate(method='linear',axis=0).fillna(0).values)))
    File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/statsmodels/tsa/seasonal.py", line 127, in seasonal_decompose
      raise ValueError("You must specify a freq or x must be a "
  ValueError: You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to None
  </code></pre>
  
  <p>Now if I add a <code>freq</code> parameter to the seasonal_decompose method such as </p>
  
  <pre><code>trend_coord = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df.iloc[:,0].values, freq=1).trend.interpolate(method='linear',axis=0).fillna(0).values)))
  </code></pre>
  
  <p>Then I get an error like </p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/souvik/Music/UI_Server2/test35.py", line 11, in &lt;module&gt;
      trend_coord = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df.iloc[:,0].values, freq=1).trend.interpolate(method='linear',axis=0).fillna(0).values)))
  AttributeError: 'numpy.ndarray' object has no attribute 'interpolate'
  </code></pre>
  
  <p>However if I get rid of any fine graining of data such as <code>interpolate</code> etc and do something like below</p>
  
  <pre><code>trend_coord = sm.tsa.seasonal_decompose(df.iloc[:,0].values, freq=1, model='additive').trend
  
  results = sm.OLS(np.asarray(trend_coord),
                   sm.add_constant(np.array([i for i in range(len(trend_coord))])), missing='drop').fit()
  slope = results.params[1]
  print("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; slope", slope)
  </code></pre>
  
  <p>Then I get a <code>slope</code> value of <code>0.13668559218559242</code>.</p>
  
  <p>But I am not sure if this is the correct way to find out the <code>slope</code> and if even the value is right.</p>
  
  <p>Is there a better way to find out <code>slope</code>?</p>