16

I have this dataframe with date time indices:

ts_log:
date    price_per_unit
2013-04-04  12.762369
2013-04-05  12.777120
2013-04-06  12.773146
2013-04-07  12.780774
2013-04-08  12.786835

I have this piece of code for decomposition

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

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

but in the line decomposition = seasonal_decompose(ts_log) i got this error :

ValueError: You must specify a freq or x must be a pandas object with a timeseries index

Where is the problem?

Reza
  • 728
  • 1
  • 10
  • 28

3 Answers3

18

After some searching i found [here][1] that, i have to add values to ts_log.price

decomposition = seasonal_decompose(ts_log.price.values, freq=30)

Edit as to comments. Adding just freq=30 is enough!

Reza
  • 728
  • 1
  • 10
  • 28
  • 1
    I had a similar error, but based on the error message, I guess adding the freq parameter did the trick. – user24981 Dec 17 '17 at 16:14
  • 6
    you fixed it not by adding by `values`, instead by adding `freq=30` – Yonas Kassa Feb 08 '18 at 23:04
  • 9
    What is "freq" doing here, exactly? – NBK Oct 03 '19 at 15:43
  • I doubt this is correct. [Docs](https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.seasonal_decompose.html) has no `freq` param, I think either you change the `freq` param of the datetime (as [this](https://stackoverflow.com/a/61593097/5931672) answer) or you use the `period` parameter. – J Agustin Barrachina Mar 20 '23 at 15:59
0

You can avoid this error by:

ts_log = ts_log.asfreq('d')

this may generate some missing values:

ts_log = ts_log.fillna(method='bfill').fillna(method='ffill')
Fares Sayah
  • 121
  • 1
  • 5
0

Following has resolved the error :

decomposition = seasonal_decompose(log_county_data , period = 30)
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107