2

I am trying to decompose seasonality and trend of my time series data using seasonal_decompose function from Statsmodels library but I am getting ValueError

Data :

                    A (Current average)
     TS 
2017-12-01 00:01:00 3.274965
2017-12-01 00:02:00 3.274083
2017-12-01 00:03:00 3.262563
2017-12-01 00:04:00 3.278352
2017-12-01 00:05:00 3.251769

Index of data :

ts_log.index

output :

DatetimeIndex(['2017-12-01 00:01:00', '2017-12-01 00:02:00',
               '2017-12-01 00:03:00', '2017-12-01 00:04:00',
               '2017-12-01 00:05:00', '2017-12-01 00:06:00',
               '2017-12-01 00:07:00', '2017-12-01 00:08:00',
               '2017-12-01 00:09:00', '2017-12-01 00:10:00',
               ...
               '2018-01-04 23:26:00', '2018-01-04 23:27:00',
               '2018-01-04 23:28:00', '2018-01-04 23:29:00',
               '2018-01-04 23:30:00', '2018-01-04 23:31:00',
               '2018-01-04 23:32:00', '2018-01-04 23:33:00',
               '2018-01-04 23:34:00', '2018-01-04 23:35:00'],
              dtype='datetime64[ns]', name='TS', length=50000, freq=None)

Decomposing seasonality

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts_log)

Error :

ValueError                                Traceback (most recent call last)
<ipython-input-79-7ca5a90bdbf8> in <module>()
      1 from statsmodels.tsa.seasonal import seasonal_decompose
----> 2 decomposition = seasonal_decompose(ts_log)
      3 
      4 trend = decomposition.trend
      5 seasonal = decomposition.seasonal

C:\Users\Paras Mani\Anaconda2\envs\py3\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided)
     82             freq = pfreq
     83         else:
---> 84             raise ValueError("You must specify a freq or x must be a "
     85                              "pandas object with a timeseries index with"
     86                              "a freq not set to None")

ValueError: You must specify a freq or x must be a pandas object with a timeseries index witha freq not set to None

Here I am using timeseries index but frequency is set to None. How can I change the frequency.

Paras Mani
  • 77
  • 2
  • 7
  • You can set the freq by `ts_log.index.freq`. This looks like a duplicate of https://stackoverflow.com/questions/34494780/time-series-analysis-unevenly-spaced-measures-pandas-statsmodels – Jarad May 07 '18 at 04:01

2 Answers2

1

If you have a pandas dataframe with a DateTimeIndex, you can inspect its frequency attribute.

print(df.index.freq)

If this returns None, you can set the frequency by assigning directly to the df.index.freq attribute or by using the df.asfreq() method. Eg.

# Set df freq to 120 min
df.index.freq = '120t'
df.asfreq('120t')

Using df.asfreq() will allow you to easily resample/convert between frequencies and handle resampling. You can find a list of valid offset aliases (the 't' part of the code) here. Or you can run pd.tseries.offsets.__all__ to get a list of valid offsets only (NB, this won't print aliases, so you'll see Minute instead of min or t).

Once your dataframe has a valid time you can run seasonal_decompose(). From experimenting in statsmodels 0.12.2, frequencies above 1 hour i.e. freq < '60T', results in ValueError: freq T not understood. Please report if you think this is in error. being thrown. If this means you'll need to downsample your df, I'd recommend using as approach along the lines of the following df.resample('1H').median().

Jason
  • 4,346
  • 10
  • 49
  • 75
-1

Try this:

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(x, model='additive', filt=None, freq=52)
fig = decomposition.plot()
plt.show()
zx485
  • 28,498
  • 28
  • 50
  • 59