Decomposition

In [11]:
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import seaborn as sns

%matplotlib inline
In [12]:
milk_prod = pd.read_csv('data/monthly-milk-production-pounds-p.csv', 
                        parse_dates = ['month'], 
                        index_col = 'month', 
                        infer_datetime_format = True)
milk_prod.columns = ['volume']
sns.tsplot(milk_prod.volume)
Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x10f667710>
In [14]:
res = sm.tsa.seasonal_decompose(milk_prod.volume)

fig = res.plot()
# fig.set_size_inches(12, 9)
In [17]:
res.trend
Out[17]:
month
1962-01-01           NaN
1962-02-01           NaN
1962-03-01           NaN
1962-04-01           NaN
1962-05-01           NaN
1962-06-01           NaN
1962-07-01    616.691667
1962-08-01    617.370833
1962-09-01    618.133333
1962-10-01    619.383333
1962-11-01    620.712500
1962-12-01    622.125000
1963-01-01    623.741667
1963-02-01    625.295833
1963-03-01    626.666667
1963-04-01    627.708333
1963-05-01    628.620833
1963-06-01    629.779167
1963-07-01    631.579167
1963-08-01    634.112500
1963-09-01    636.929167
1963-10-01    639.712500
1963-11-01    642.212500
1963-12-01    644.204167
1964-01-01    645.787500
1964-02-01    647.425000
1964-03-01    649.212500
1964-04-01    651.083333
1964-05-01    653.295833
1964-06-01    656.000000
                 ...    
1972-07-01    852.279167
1972-08-01    852.145833
1972-09-01    851.929167
1972-10-01    851.558333
1972-11-01    851.308333
1972-12-01    850.679167
1973-01-01    849.683333
1973-02-01    848.412500
1973-03-01    846.616667
1973-04-01    844.779167
1973-05-01    843.745833
1973-06-01    843.366667
1973-07-01    843.529167
1973-08-01    844.041667
1973-09-01    844.512500
1973-10-01    844.929167
1973-11-01    845.591667
1973-12-01    847.054167
1974-01-01    849.129167
1974-02-01    851.462500
1974-03-01    854.004167
1974-04-01    856.179167
1974-05-01    857.591667
1974-06-01    858.591667
1974-07-01           NaN
1974-08-01           NaN
1974-09-01           NaN
1974-10-01           NaN
1974-11-01           NaN
1974-12-01           NaN
Name: volume, Length: 156, dtype: float64
In [ ]:
?sm.tsa.seasonal_decompose
In [18]:
import pandas as pd
import seaborn as sns
%matplotlib inline
trees = pd.read_csv('data/tree-rings.csv', parse_dates = ['year'], index_col = 'year', infer_datetime_format = True)
trees.head()
sns.tsplot(trees.rings)
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x1113ac710>
In [ ]: