14

I have obtained time-series data this way:

from pandas.io.data import DataReader
from datetime import datetime
ts_log = DataReader('RUB=X',  'yahoo', datetime(2007,1,1), datetime(2016,8,30))["Adj Close"]

ts_log looks this way:

Date
2007-01-01    3.269759
2007-01-02    3.269759
2007-01-03    3.270519
  .....        ......

Then I want to get decomposition by using statsmodels:

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

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

To fix this I tried 2 approaches:

1) That won't work, because not every day has a value:

ts_log = pd.DataFrame(data=ts_log.values, index=pd.date_range(start=datetime(2007, 1, 1), 
                                                              end = datetime(2016,8,30), freq='D'), columns=['usd/rub'])

2) I thought that casting index to datetime should work , but no:

ts_log = pd.DataFrame(data=ts_log.values, index=pd.to_datetime(ts_log.index), columns=['usd/rub'])

Exactly the same error.

How can I make things to work?

P.S. This code is pretty fine, but I have a bit different data, anyway it should help you catch up the idea:

length = 400
x = np.sin(np.arange(length)) * 10 + np.random.randn(length)
df = pd.DataFrame(data=x, index=pd.date_range(start=datetime(2015, 1, 1), periods=length, freq='w'), columns=['value'])
decomposition = seasonal_decompose(df)
Rocketq
  • 5,423
  • 23
  • 75
  • 126
  • What error does your second attempt give? The one with `pd.to_datetime(ts_log.index)`. – IanS Sep 01 '16 at 13:46
  • @IanS Calling seasonal_decompose(df) just after that return same error `You must specify a freq or x must be a pandas object with a timeseries index` . I cant set freq here( – Rocketq Sep 01 '16 at 14:05
  • 3
    This should help you: http://stackoverflow.com/q/34494780/5276797 – IanS Sep 01 '16 at 14:11
  • 5
    @IanS Thank you for the link - `decomposition = seasonal_decompose(ts_log, freq = 52)` - now it works – Rocketq Sep 01 '16 at 14:16
  • 1
    But what does `freq = 52` - mean? – Rocketq Sep 01 '16 at 14:28
  • Unfortunately I don't know this function... But a quick Google search (for 'seasonal_decompose freq') shows that you're not the only one having difficulty understanding this parameter. – IanS Sep 01 '16 at 15:05
  • 3
    From my understanding, the `freq` depends on the type of your Time Series. As in, if it is daily (e.g.: `2018-09-05`), means 52 days. If is set to monthly (`2018-09 | or 2018-Sep` means 52 months, and so on... – mrbTT Sep 05 '18 at 17:17
  • 1
    `freq` is deprecated, use `period` instead. – Sander Heinsalu Jan 03 '21 at 16:12

0 Answers0