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

1

6

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.

Eijkman answered 23/2, 2017 at 20:40 Comment(2)
github.com/statsmodels/statsmodels/issues/3503 This is currently a bug in 0.8. Workaround: use numpy arrays in the function call if there is no time index.Frannie
Hi Josef, I filed that issue, thanks a lot for the detailed responses on github. Happy to continue to follow this there.Eijkman
E
12

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)

Eijkman answered 24/2, 2017 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.