Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 360
Max Power Avatar asked Feb 23 '17 20:02

Max Power


1 Answers

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)

like image 119
Max Power Avatar answered Sep 28 '22 08:09

Max Power