python print values seasonal_decomposition

3

I am totally beginner in Python and after using seasonal_decompose for time series decomposition result=seasonal_decompose(series, model='additive', freq=365) I got plotted results with commands result.plot() and pyplot.show(), but I cannot understand how to print this results values on screen or how to see decomposed time series values? I have plot, but I need to have values in console or some data file.

Complete code:

from pandas import read_csv
series=read_csv('B.csv', header=0, parse_dates=[0], index_col=0, squeeze=True)
from random import randrange
from matplotlib import pyplot
from statsmodels.tsa.seasonal import seasonal_decompose
result=seasonal_decompose(series, model='additive', freq=365)
result.plot()
pyplot.show()

nick_name

Posted 2017-07-26T16:05:46.343

Reputation: 85

Answers

5

seasonal_decompose returns an 'object with seasonal, trend, and resid attributes.' We can access the data by calling the object:

res = seasonal_decompose(series, model='additive', freq=365)
residual = res.resid
seasonal = res.seasonal 
trend = res.trend
print trend

etc...

Hobbes

Posted 2017-07-26T16:05:46.343

Reputation: 1 329

Seasonal and trend works for me, but with residual = res.residual I got answer Attribute Error: 'DecomposeResult' object has no attribute 'residual' – nick_name – 2017-07-26T16:24:20.693

2Try res.resid (inconsistent API) – Emre – 2017-07-26T16:40:58.510

1

My bad, as it is in the docs, res.resid should work. Thanks @Emre. http://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.seasonal_decompose.html

– Hobbes – 2017-07-26T16:42:20.003

It works now. But I have some other problem, I got NaN values for trend and resid. Besides that, on plot I got some real values for trend and residuals, I can't understand what is different here in console output? – nick_name – 2017-07-26T16:48:30.560

2Are NaN at beginning and end of the arrays? This is probably because of the moving average filtering method. For example, a two week moving average will result in two weeks of NaNs initially. – Hobbes – 2017-07-26T16:55:37.380

No, I have time series consisted of daily values for seven years, so around 2000 values, without missing any data. Can that be also because of moving average filtering? And I got good values on plot, but when I print result in console I get NaN values. – nick_name – 2017-07-26T17:06:41.967

I didn't see, because python printed only couple of the first and lasts rows, that there are some values in the middle of residuals and trend vector, only some values at the begin and the end of vector are NaN values, I will search for detailf of moving average filtering if that is the problem. The number of NaN values is half of frequency on each side of vector, I think. – nick_name – 2017-07-27T14:43:23.710