Skip to content

seasonal_decompose() With Known Freq but Without DatetimeIndex() #3503

Closed

Description

@MaxPowerWasTaken

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.

Activity

  1. josef-pkt commented on Feb 24, 2017

    @josef-pkt
    Member

    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)

  2. josef-pkt commented on Feb 24, 2017

    @josef-pkt
    Member

    OK, it's in the wrapper code
    from .filters._utils import _maybe_get_pandas_wrapper_freq

    accesses the attribute inferred_freq without protection, even if we don't need the freq

    adding prio-high because it's a popular function

  3. josef-pkt commented on Feb 24, 2017

    @josef-pkt
    Member

    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.

  4. josef-pkt commented on Feb 24, 2017

    @josef-pkt
    Member

    Also, if we release an 0.8.1, then this would be a backport candidate

  5. MaxPowerWasTaken commented on Feb 24, 2017

    @MaxPowerWasTaken
    Author

    Thanks Josef,

    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).

  6. josef-pkt commented on Feb 24, 2017

    @josef-pkt
    Member

    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

    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
    
  7. MaxPowerWasTaken commented on Feb 24, 2017

    @MaxPowerWasTaken
    Author

    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.

  8. added a commit that references this issue on Oct 9, 2017
    52ab10c
  9. added a commit that references this issue on Nov 4, 2017
    de882e9
  10. yulingchen54 commented on Apr 4, 2018

    @yulingchen54

    Hi All,

    How do we pick up the fix of this issue? I'm using statsmodels 0.8.0 and saw this issue?

  11. MaxPowerWasTaken commented on Apr 4, 2018

    @MaxPowerWasTaken
    Author

    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.

  12. rajendrasn commented on Sep 18, 2018

    @rajendrasn

    The below code worked for me in Python 3

    decomposition = sm.tsa.seasonal_decompose(series['Column Name'].values,freq=3)

  13. josef-pkt commented on Sep 18, 2018

    @josef-pkt
    Member

    @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.

    decomposition = sm.tsa.seasonal_decompose(series['Column Name'], freq=3)

  14. added this to the 0.9 milestone on Sep 18, 2018
  15. rajendrasn commented on Sep 19, 2018

    @rajendrasn

    @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.

    decomposition = sm.tsa.seasonal_decompose(series['Column Name'], freq=3)

    Hi Josef,
    your are right. .values is not necessary.

  16. pajoshi commented on Aug 14, 2019

    @pajoshi

    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

  17. manav3475 commented on Aug 13, 2021

    @manav3475

    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
    Screenshot (294)

    import statsmodels
    print(statsmodels.version)
    0.12.0

    please any one help me to slove this

    Screenshot (293)

  18. bashtage commented on Aug 13, 2021

    @bashtage
    Member

    You need to explicitly set the period parameter to let the function know how often the seasonal cycle repeats.

  19. manav3475 commented on Aug 13, 2021

    @manav3475

    You need to explicitly set the period parameter to let the function know how often the seasonal cycle repeats.

    what should be a period parameter value of the function and how to set it ? can you give me some example?

  20. manav3475 commented on Aug 13, 2021

    @manav3475

    See the docs: https://www.statsmodels.org/stable/generated/statsmodels.tsa.seasonal.seasonal_decompose.html

    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 ?

  21. bashtage commented on Aug 13, 2021

    @bashtage
    Member

    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.

  22. manav3475 commented on Aug 13, 2021

    @manav3475

    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.

    Thank you So much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions