title: linear regression for timeseries python (numpy or pandas)
body: <p>I am new to python and programming in general, so forgive any simple mistakes/ things that should be obvious. </p>
  
  <p>What I am trying to do is quite simple, I just want to fit a linear trend (1-d polynomial) to a bunch of time-series to see whether the slopes are positive or negative. Right now I am just trying to get it to work for one time series. </p>
  
  <p>The problem: it seems like both pandas and numpy can't do regressions for datetimes. My date times are not regular (generally 1 day per month but not the same day) so can't use the suggestion posed in <a href="https://stackoverflow.com/questions/30425490/linear-regression-from-time-series-pandas/30431930#30431930">Linear Regression from Time Series Pandas</a> </p>
  
  <p>My time series csv looks like:</p>
  
  <pre><code>StationName,    year,   month,  day,    depth,  NO3-N,  PO4-P,  TotP,   TotN,
  Kvarnbacken (Savaran),  2003,   2,  25, 0.5,    46, 9,  14, 451
  Kvarnbacken (Savaran),  2003,   3,  18, 0.5,    64, 15, 17, 310
  Kvarnbacken (Savaran),  2003,   3,  31, 0.5,    76, 7,  19, 566
  </code></pre>
  
  <p>so far what i have is </p>
  
  <pre><code>import datetime as dt
  from scipy import stats
  import numpy as np
  
  # read in station csv file
  data = pd.read_csv('Kvarnbacken (Savaran)_2003.csv')
  data.head()
  # set up dates to something python can recognize
  data['date'] = pd.to_datetime(data.year*10000+data.month *
                            100+data.day, format='%Y%m%d')
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>slope, intercept, r_value, p_value, std_err = stats.linregress(data.date,
                                                                 data.TotP)
  </code></pre>
  
  <p>and got the error
      TypeError: ufunc add cannot use operands with types dtype('
  
  <p>I also tried</p>
  
  <pre><code>coefP = np.polyfit(data.date, data.TotP, 1)
  polyP = np.poly1d(coefP)
  ys = polyP(data.date)
  print 'For P: coef, poly'
  print coefP
  print polyP
  </code></pre>
  
  <p>and got the same error. </p>
  
  <p>I am guessing the easiest way around this is to do something where I just count the days since the first measurement I have and then just do a regression with days_since to the total phosphorous concentration (totP) but I am not sure of the easiest way to do that or if there was another trick. </p>