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)