What am I doing wrong when using seasonal decompose in Python?
<p>I have a small time series with monthly intervals. I wanted to plot it and then decompose into seasonality, trend, residuals. I start by importing csv into pandas and than plotting just the time series which works fine. I follow <a href="http://www.cbcity.de/timeseries-decomposition-in-python-with-statsmodels-and-pandas" rel="nofollow">This</a> tutorial and my code goes like this:</p>

<pre><code>%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

ali3 = pd.read_csv('C:\\Users\\ALI\\Desktop\\CSV\\index\\ZIAM\\ME\\ME_DATA_7_MONTH_AVG_PROFIT\\data.csv',
 names=['Date', 'Month','AverageProfit'],
 index_col=['Date'],
 parse_dates=True)

\* Delete month column which is a string */
del ali3['Month']


ali3
plt.plot(ali3)
</code></pre>

<p><a href="http://i.stack.imgur.com/aqSiu.jpg" rel="nofollow">Data Frame</a></p>

<p>At this stage I try to do the seasonal decompose like this:</p>

<pre><code>import statsmodels.api as sm 
res = sm.tsa.seasonal_decompose(ali3.AverageProfit)  
fig = res.plot() 
</code></pre>

<p>which results in the following error:</p>

<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-41-afeab639d13b&gt; in &lt;module&gt;()
      1 import statsmodels.api as sm
----&gt; 2 res = sm.tsa.seasonal_decompose(ali3.AverageProfit)
      3 fig = res.plot()

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq)
     86             filt = np.repeat(1./freq, freq)
     87 
---&gt; 88     trend = convolution_filter(x, filt)
     89 
     90     # nan pad for conformability - convolve doesn't do it

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\statsmodels\tsa\filters\filtertools.py in convolution_filter(x, filt, nsides)
    287 
    288     if filt.ndim == 1 or min(filt.shape) == 1:
--&gt; 289         result = signal.convolve(x, filt, mode='valid')
    290     elif filt.ndim == 2:
    291         nlags = filt.shape[0]

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in convolve(in1, in2, mode)
    468         return correlate(volume, kernel[slice_obj].conj(), mode)
    469     else:
--&gt; 470         return correlate(volume, kernel[slice_obj], mode)
    471 
    472 

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in correlate(in1, in2, mode)
    158 
    159     if mode == 'valid':
--&gt; 160         _check_valid_mode_shapes(in1.shape, in2.shape)
    161         # numpy is significantly faster for 1d
    162         if in1.ndim == 1 and in2.ndim == 1:

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in _check_valid_mode_shapes(shape1, shape2)
     70         if not d1 &gt;= d2:
     71             raise ValueError(
---&gt; 72                 "in1 should have at least as many items as in2 in "
     73                 "every dimension for 'valid' mode.")
     74 

ValueError: in1 should have at least as many items as in2 in every dimension for 'valid' mode.
</code></pre>

<p>Can anyone shed some light on what I'm doing wrong and how may I fix it? much obliged.</p>

<p><strong>Edit:</strong>  Thats how the data frame looks like</p>

<pre><code>Date            AverageProfit

2015-06-01          29.990231
2015-07-01          26.080038
2015-08-01          25.640862
2015-09-01          25.346447
2015-10-01          27.386001
2015-11-01          26.357709
2015-12-01          25.260644
</code></pre>

<p>You have 7 data points, that is usually a very small number for performing stationarity analysis.</p>

<p>You don't have enough points to use seasonal decomposition. To see this, you can concatenate your data to create an extended time series (just repeating your data for the following months). Let <code>extendedData</code> be this extended dataframe and <code>data</code> your original data.</p>

<pre><code>data.plot()
</code></pre>

<p><a href="http://i.stack.imgur.com/TNi9J.png" rel="nofollow"><img src="http://i.stack.imgur.com/TNi9J.png" alt="enter image description here"></a></p>

<pre><code>extendedData.plot()
</code></pre>

<p><a href="http://i.stack.imgur.com/m9Pdt.png" rel="nofollow"><img src="http://i.stack.imgur.com/m9Pdt.png" alt="enter image description here"></a></p>

<pre><code>res = sm.tsa.seasonal_decompose(extendedData.interpolate())
res.plot()
</code></pre>

<p><a href="http://i.stack.imgur.com/fl6lN.png" rel="nofollow"><img src="http://i.stack.imgur.com/fl6lN.png" alt="enter image description here"></a></p>

<p>The frequency (<code>freq</code>) for the seasonal estimate is automatically estimated form the data, and can be manually specified.</p>

<hr>

<p>You can try to take a first difference: generate a new time series subtracting each data value from the previous one. In your case it looks like this:</p>

<p><a href="http://i.stack.imgur.com/p8bBQ.png" rel="nofollow"><img src="http://i.stack.imgur.com/p8bBQ.png" alt="enter image description here"></a></p>

<p>An stationarity test can be applied next, as explained <a href="http://stats.stackexchange.com/questions/120270/how-do-i-detrend-time-series">here</a></p>

python pandas jupyter