Using statsmodels.seasonal_decompose() without DatetimeIndex but with Known Frequency

我的未来我决定 提交于 2019-12-10 14:33:58

问题


I have a time-series signal I would like to decompose in Python, so I turned to statsmodels.seasonal_decompose(). My data has frequency of 48 (half-hourly). I was getting the same error as this questioner, where the solution was to change from an Int index to a DatetimeIndex. But I don't know the actual dates/times my data is from.

In this github thread, one of the statsmodels contributors says that

"In 0.8, you should be able to specify freq as keyword argument to override the index."

But this seems not to be the case for me. Here is a minimal code example illustrating my issue:

import statsmodels.api as sm
dta = pd.Series([x%3 for x in range(100)])
decomposed = sm.tsa.seasonal_decompose(dta, freq=3)

AttributeError: 'RangeIndex' object has no attribute 'inferred_freq'

Version info:

import statsmodels
print(statsmodels.__version__)
0.8.0

Is there a way to decompose a time-series in statsmodels with a specified frequency but without a DatetimeIndex?

If not, is there a preferred alternative for doing this in Python? I checked out the Seasonal package, but its github lists 0 downloads/month, one contributor, and last commit 9 months ago, so I'm not sure I want to rely on that for my project.


回答1:


Thanks to josef-pkt for answering this on github. There is a bug in statsmodels 0.8.0 where it always attempts to calculate an inferred frequency based on a DatetimeIndex, if passed a Pandas object.

The workaround when using Pandas series is to pass their values in a numpy array to seasonal_decompose(). For example:

import statsmodels.api as sm

my_pandas_series = pd.Series([x%3 for x in range(100)])
decomposed = sm.tsa.seasonal_decompose(my_pandas_series.values, freq=3)

(no errors)



来源:https://stackoverflow.com/questions/42425774/using-statsmodels-seasonal-decompose-without-datetimeindex-but-with-known-freq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!