Skip to content

BUG: TSA models if date index without known frequency #3068

Closed

Description

@ChadFulton

If given a Pandas endog, TSA models require the index to contain dates. If the date index does not have a frequency, an attempt is made to infer it, and it appears that the intent is to raise an error if this inference fails (statsmodels/tsa/base/tsa_model.py:56):

if not freq:
    try:
        freq = datetools._infer_freq(dates)
    except:
        raise ValueError("Frequency inference failed. Use `freq` "
                "keyword.")

However, _infer_freq doesn't usually raise an exception (from a brief look, it only ever raises an exception if the length of the series is less than 3), but it will often return None if the frequency can't be inferred.

I'm guessing we want to raise an exception here if the inferred frequency is None. I'll make a PR to add that behavior, unless I've misunderstood.

Activity

  1. ChadFulton commented on Jun 22, 2016

    @ChadFulton
    MemberAuthor

    Although I guess this would be backwards incompatible.

  2. ChadFulton commented on Jul 16, 2016

    @ChadFulton
    MemberAuthor

    The main issue is that it allowed coercing generic indexes into DatetimeIndex using pd.to_datetime. The problem with this approach is the following:

    > pd.to_datetime(pd.Index([1,2,3]))
    DatetimeIndex(['1970-01-01 00:00:00.000000001',
                   '1970-01-01 00:00:00.000000002',
                   '1970-01-01 00:00:00.000000003'],
                  dtype='datetime64[ns]', freq=None)

    So if someone did the following:

    endog = sm.datasets.macrodata.load_pandas().data['realgdp']
    mod = sm.tsa.SARIMAX(endog, order=(1, 1, 0))
    res = mod.fit()
    res.forecast(steps=12)

    they get:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-f780a15a32e4> in <module>()
          2 mod = sm.tsa.SARIMAX(endog, order=(1, 1, 0))
          3 res = mod.fit()
    ----> 4 res.forecast(steps=12)
    
    ...
    
    ValueError: Wrong number of items passed 12, placement implies 11
    
  3. ChadFulton commented on Jul 16, 2016

    @ChadFulton
    MemberAuthor

    The actual problem here is that for some reason, DatetimeIndex, when called with start, end, and a nanosecond frequency doesn't actually include end.

    > start = pd.Timestamp(1)
    > end = pd.Timestamp(4)
    > pd.DatetimeIndex(start=start, end=end, freq='N')
    DatetimeIndex(['1970-01-01 00:00:00.000000001',
                   '1970-01-01 00:00:00.000000002',
                   '1970-01-01 00:00:00.000000003'],
                  dtype='datetime64[ns]', freq='N')

    whereas a similar call with annual frequency gives:

    > start = pd.Timestamp('1971')
    > end = pd.Timestamp('1974')
    > pd.DatetimeIndex(start=start, end=end, freq='AS')
    DatetimeIndex(['1971-01-01',
                   '1972-01-01',
                   '1973-01-01',
                   '1974-01-01'],
                  dtype='datetime64[ns]', freq='AS-JAN')
  4. ChadFulton commented on Jul 16, 2016

    @ChadFulton
    MemberAuthor

    Taking a look at test_pandas_nodates_index in test_base.py I think it's pretty clear that the intention was that not to allow any Pandas input that did not specifically have a date index.

    However, the existing behavior allows estimating a model with a pandas endog, even though forecasting will fail. So I think the best course is to fix the forecasting problem by putting a shim in for the nanosecond case. That way, previous code doesn't break and the bug gets fixed. See #3113

    On the other hand, I don't think people intent for a pandas index of 0, 1, ... to turn into a DatetimeIndex of 1970-01-01 00:00:00.000000001, 1970-01-01 00:00:00.000000002, ...

    @josef-pkt @jseabold any thoughts on the best course of action for the 0.8 release? Deprecation? Warning? Exception?

  5. josef-pkt commented on Jul 16, 2016

    @josef-pkt
    Member

    That seems to be more a pandas bug, maybe inherited open upper interval from numpy/python.

    I think adding a workaround is good, but I think we should check changes in behavior of recent pandas, and maybe stick with an exception that we had with earlier versions, IIRC.
    IIRC, @bashtage was fixing the try except with datetools._infer_freq somewhere because it didn't raise exceptions (anymore).

    (I'm very distracted by family, and have problems concentrating on any statsmodels problems.)

  6. ChadFulton commented on Jul 16, 2016

    @ChadFulton
    MemberAuthor

    (I'm very distracted by family, and have problems concentrating on any statsmodels problems.)

    No problem, there's no rush on any of this. If I ping you, feel free to ignore until you have time.

  7. ChadFulton commented on Jul 16, 2016

    @ChadFulton
    MemberAuthor

    My opinion on the best course of action for v0.8 is the following:

    1. Include the shim fix so that forecasting works, and maintain existing (broken) behavior which allows giving an endog as a pandas Series if it has some index that can be converted via pd.to_datetime() to a DatetimeIndex (even if the resulting DatetimeIndex is nonsense).
    2. Add a deprecation warning to the datetools._infer_freq try except block if the inferred frequency is None that the only supported functionality is pandas Series that already have real date time indexes, and other cases are not guaranteed to work in the future.
    3. Then for v0.9 add support (if possible) also for Int64Index; as I understand it, this index should be pretty much like working with Numpy arrays (except for the possibility of missing index values).
  8. modified the milestones: 0.9, 0.8 on Aug 19, 2016
  9. josef-pkt commented on Aug 19, 2016

    @josef-pkt
    Member

    AFAIU:
    automatic conversion of int to datetime raises in 0.8
    we still need to merge the workaround for missing end date with ns for 0.8
    Chad wrote a proposal for general refactoring of date and time handling.

  10. ChadFulton commented on Aug 20, 2016

    @ChadFulton
    MemberAuthor

    Yes, I think you're right. I have rebased the PR, and created a backport PR also.

  11. added 2 commits that reference this issue on Sep 21, 2016
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