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
BUG: TSA models if date index without known frequency #3068
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):
ifnotfreq:
try:
freq=datetools._infer_freq(dates)
except:
raiseValueError("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.
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?
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.)
My opinion on the best course of action for v0.8 is the following:
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).
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.
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).
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.
Activity
ChadFulton commentedon Jun 22, 2016
Although I guess this would be backwards incompatible.
ChadFulton commentedon Jul 16, 2016
The main issue is that it allowed coercing generic indexes into
DatetimeIndexusingpd.to_datetime. The problem with this approach is the following:So if someone did the following:
they get:
ChadFulton commentedon Jul 16, 2016
The actual problem here is that for some reason,
DatetimeIndex, when called withstart,end, and a nanosecond frequency doesn't actually includeend.whereas a similar call with annual frequency gives:
ChadFulton commentedon Jul 16, 2016
Taking a look at
test_pandas_nodates_indexintest_base.pyI 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?
josef-pkt commentedon Jul 16, 2016
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_freqsomewhere because it didn't raise exceptions (anymore).(I'm very distracted by family, and have problems concentrating on any statsmodels problems.)
ChadFulton commentedon Jul 16, 2016
No problem, there's no rush on any of this. If I ping you, feel free to ignore until you have time.
ChadFulton commentedon Jul 16, 2016
My opinion on the best course of action for v0.8 is the following:
endogas a pandas Series if it has some index that can be converted viapd.to_datetime()to a DatetimeIndex (even if the resulting DatetimeIndex is nonsense).datetools._infer_freqtry except block if the inferred frequency isNonethat 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.Int64Index; as I understand it, this index should be pretty much like working with Numpy arrays (except for the possibility of missing index values).josef-pkt commentedon Aug 19, 2016
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.
ChadFulton commentedon Aug 20, 2016
Yes, I think you're right. I have rebased the PR, and created a backport PR also.
Merge pull request #3113 from ChadFulton/gh-3068
Merge pull request #3175 from ChadFulton/gh-3068-backport