You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
seasonal_decompose() With Known Freq but Without DatetimeIndex() #3503
In issue #3225, someone wrote "In 0.8, you should be able to specify freq as keyword argument to override the index." But I seem to still be having the same problems as the questioner there, with my data.
Here is a simple reproducible code example of 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'
import statsmodels
print(statsmodels.__version__)
0.8.0
Should it be possible to use StatsModels seasonal_decompose() with a given frequency but without a DatetimeIndex? With my real data, I know it is of half-hour resolution, but I don't know the dates/times it corresponds to, so there's no neat/clean way of mapping it to a datetime index.
I've also posted this question on StackOverflow and will add an answer there if I get one here. Thanks everyone for your time.
my guess for a solution (based on a brief look) is that we should use either one of the following depending on whether freq is specified:
_pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)
or _pandas_wrapper, pfreq = _maybe_get_pandas_wrapper(x)
The if freq is further down in the function, so there might be some reshuflling needed.
or we just change _maybe_get_pandas_wrapper_freq to return pfreq is None if attribute is not available, instead of letting it raise an exception
note getting freq is the only difference between the two _maybe_get_pandas_wrapper functions.
Looking over the source code, the first line of seasonal_decompose is:
_pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)
and then in _maybe_get_pandas_wrapper_freq, if the x that was passed is a Pandas object, this line of code is always executed: freq = index.inferred_freq
Does the following seem like a viable solution to you?
What if seasonal_decompose also passed its own freq (None unless the caller specified one, as I did in this issue) to _maybe_get_pandas_wrapper_freq? Then the line of code I posted above could be changed to:
if freq is not None:
freq = index.inferred_freq
_maybe_get_pandas_wrapper_freq could still have the same return statement: return func, freq
it would just return the freq it was given (if it was given one), or the freq it inferred if it was not given one (if it was given freq=None).
But I think all these solutions are a bit to roundabout for my. We missed this when we changed the behavior if freq is defined because that check is hiding in a utility function.
If possible, I prefer a solution where the behavior becomes clear from looking at seasonal_compose.
E.g. there is still the other if freq is None further down in seasonal compose.
If possible I would like a refactor to use something like this at the beginning of seasonal_decompose
if freq is not None:
don't look at index at all (i.e. keep it just so we can return a pandas Series)
else:
use implied freq or raise
That makes sense to me Josef. I agree the cleaner solution would be to move up the check for freq within seasonal_compose() as you suggest. I'll use your .values workaround for now. Thanks again.
@rajendrasn Is .values, i.e. conversion to numpy array still necessary?
This was supposed to be fixed so that the freq keyword overrides all period/freq checking in the pandas Series.
If it doesn't work with 0.9.0, then there is still a bug.
@rajendrasn Is .values, i.e. conversion to numpy array still necessary?
This was supposed to be fixed so that the freq keyword overrides all period/freq checking in the pandas Series.
If it doesn't work with 0.9.0, then there is still a bug.
Hello,
What is the ideal value of freq for time series data?
I'm still getting the error: ValueError: You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to None
I have timeseries data with daily updates.
Any help is appreciated.
Regards and thanks
-Pratima
What is the importance of period parameter in seasonal_decompose() funcion ? I've set its value to 1 and the error is resolved but I still not understood what happened. So can you please give me example with different period values or how to find ideal value for it ?
The period determines how often the cycle repeats in the seasonal component. For example, with monthly data period would usually be 12. With hourly it could be 24 (daily) or 168 (weekly). This is something that you should know about your data. Or you can try alternative plausible values when doing EDA.
The period determines how often the cycle repeats in the seasonal component. For example, with monthly data period would usually be 12. With hourly, it could be 24 (daily) or 168 (weekly). This is something that you should know about your data. Or you can try alternative plausible values when doing EDA.
Activity
josef-pkt commentedon Feb 24, 2017
I haven't checked yet what is going on, but I get the same exception with master. I thought the index would be completely ignored.
workaround is to use a numpy array, i.e. either .values or np.asarray:
decomposed = sm.tsa.seasonal_decompose(np.asarray(dta), freq=3)josef-pkt commentedon Feb 24, 2017
OK, it's in the wrapper code
from .filters._utils import _maybe_get_pandas_wrapper_freqaccesses the attribute
inferred_freqwithout protection, even if we don't need the freqadding prio-high because it's a popular function
josef-pkt commentedon Feb 24, 2017
my guess for a solution (based on a brief look) is that we should use either one of the following depending on whether
freqis specified:_pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)or
_pandas_wrapper, pfreq = _maybe_get_pandas_wrapper(x)The if
freqis further down in the function, so there might be some reshuflling needed.or we just change
_maybe_get_pandas_wrapper_freqto return pfreq is None if attribute is not available, instead of letting it raise an exceptionnote getting freq is the only difference between the two _maybe_get_pandas_wrapper functions.
josef-pkt commentedon Feb 24, 2017
Also, if we release an 0.8.1, then this would be a backport candidate
MaxPowerWasTaken commentedon Feb 24, 2017
Thanks Josef,
Looking over the source code, the first line of
seasonal_decomposeis:_pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)and then in
_maybe_get_pandas_wrapper_freq, if thexthat was passed is a Pandas object, this line of code is always executed:freq = index.inferred_freqDoes the following seem like a viable solution to you?
What if
seasonal_decomposealso passed its ownfreq(Noneunless the caller specified one, as I did in this issue) to_maybe_get_pandas_wrapper_freq? Then the line of code I posted above could be changed to:_maybe_get_pandas_wrapper_freqcould still have the same return statement:return func, freqit would just return the
freqit was given (if it was given one), or thefreqit inferred if it was not given one (if it was givenfreq=None).josef-pkt commentedon Feb 24, 2017
It's also a possible solution.
But I think all these solutions are a bit to roundabout for my. We missed this when we changed the behavior if freq is defined because that check is hiding in a utility function.
If possible, I prefer a solution where the behavior becomes clear from looking at seasonal_compose.
E.g. there is still the other if freq is None further down in seasonal compose.
If possible I would like a refactor to use something like this at the beginning of seasonal_decompose
MaxPowerWasTaken commentedon Feb 24, 2017
That makes sense to me Josef. I agree the cleaner solution would be to move up the check for freq within
seasonal_compose()as you suggest. I'll use your .values workaround for now. Thanks again.BUG: seasonal_decompose, don't check inferred_freq if freq given, closes
BUG: seasonal_decompose, don't check inferred_freq if freq given, closes
yulingchen54 commentedon Apr 4, 2018
Hi All,
How do we pick up the fix of this issue? I'm using statsmodels 0.8.0 and saw this issue?
MaxPowerWasTaken commentedon Apr 4, 2018
Here's the solution I used, provided by josef-pkt:
https://stackoverflow.com/a/42445953/1870832
Based on the link above for the merged bugfix ('BUG: seasonal_decompose, don't check inferred_freq if freq given, #4007'; https://github.com/statsmodels/statsmodels/pull/4007)...it looks like this should be fixed in v0.9, although it also appears to me that v0.9 is not released yet.
rajendrasn commentedon Sep 18, 2018
The below code worked for me in Python 3
decomposition = sm.tsa.seasonal_decompose(series['Column Name'].values,freq=3)
josef-pkt commentedon Sep 18, 2018
@rajendrasn Is
.values, i.e. conversion to numpy array still necessary?This was supposed to be fixed so that the
freqkeyword overrides all period/freq checking in the pandas Series.If it doesn't work with 0.9.0, then there is still a bug.
decomposition = sm.tsa.seasonal_decompose(series['Column Name'], freq=3)rajendrasn commentedon Sep 19, 2018
Hi Josef,
your are right. .values is not necessary.
pajoshi commentedon Aug 14, 2019
Hello,
What is the ideal value of freq for time series data?
I'm still getting the error: ValueError: You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to None
I have timeseries data with daily updates.
Any help is appreciated.
Regards and thanks
-Pratima
bashtage commentedon Aug 14, 2019
You should use the seasonal_decompose in master. You can set
periodto be what every period your series has (e.g., 365 days).http://www.statsmodels.org/devel/generated/statsmodels.tsa.seasonal.seasonal_decompose.html?highlight=seasonal_decompose#statsmodels.tsa.seasonal.seasonal_decompose
manav3475 commentedon Aug 13, 2021
Hii,
My Statsmodels version is 0.12.0 but my data is also does not fit into the seasonal_decompose function it will give me error that
You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None

import statsmodels
print(statsmodels.version)
0.12.0
please any one help me to slove this
bashtage commentedon Aug 13, 2021
You need to explicitly set the
periodparameter to let the function know how often the seasonal cycle repeats.manav3475 commentedon Aug 13, 2021
what should be a period parameter value of the function and how to set it ? can you give me some example?
bashtage commentedon Aug 13, 2021
See the docs: https://www.statsmodels.org/stable/generated/statsmodels.tsa.seasonal.seasonal_decompose.html
manav3475 commentedon Aug 13, 2021
What is the importance of period parameter in seasonal_decompose() funcion ? I've set its value to 1 and the error is resolved but I still not understood what happened. So can you please give me example with different period values or how to find ideal value for it ?
bashtage commentedon Aug 13, 2021
The period determines how often the cycle repeats in the seasonal component. For example, with monthly data period would usually be 12. With hourly it could be 24 (daily) or 168 (weekly). This is something that you should know about your data. Or you can try alternative plausible values when doing EDA.
manav3475 commentedon Aug 13, 2021
Thank you So much
ashioyajotham commentedon Dec 6, 2022
This is the perfect answer