Repository: statsmodels/statsmodels

Issue:
DataFrame with integer names not handled in ARIMA
```
from datetime import datetime
import pandas as pd
import statsmodels.api as sm

vals = [96.2, 98.3, 99.1, 95.5, 94.0, 87.1, 87.9, 86.7402777504474]

dr = pd.date_range(datetime.today(), periods=len(vals), freq='AS')
ts = pd.TimeSeries(vals, index=dr)
df = pd.DataFrame(ts)
model = sm.tsa.ARIMA(df,(2,0,2)).fit()

File "scripts/arima_forecast.py", line 85, in 
model = ARIMA(df,(2,0,2), freq='AS').fit()
File "/Library/Python/2.7/site-packages/statsmodels-0.5.0-py2.7-macosx-10.8-intel.egg/statsmodels/tsa/arima_model.py", line 806, in fit
self.exog_names)
File "/Library/Python/2.7/site-packages/statsmodels-0.5.0-py2.7-macosx-10.8-intel.egg/statsmodels/tsa/arima_model.py", line 312, in _make_arma_names
ar_lag_names = util.make_lag_names([data.ynames], k_ar, 0)
File "/Library/Python/2.7/site-packages/statsmodels-0.5.0-py2.7-macosx-10.8-intel.egg/statsmodels/tsa/vector_ar/util.py", line 63, in make_lag_names
lag_names.append('L'+str(i)+'.'+name)
TypeError: cannot concatenate 'str' and 'numpy.int64' objects
```

Reported in this comment by @debovis https://github.com/statsmodels/statsmodels/commit/b9cc59420d128f88f56823871ac00a5360062017#commitcomment-3840566

Not sure if this is a bug yet. I suspect it may be, but if you just pass in the TimeSeries there's a lag order error. Should be able to get initial estimates for a 2,2 model with 8 observations.



Discussion:
The lag order 'bug' is due to the fact that the lag-order selection in AR for the start params is 6 and there's only 8 observations. Really not sure how much of a real use-case this is to warrant a fix, but I guess I'll throw in another check.


Resolving patch:
diff --git a/statsmodels/tsa/arima_model.py b/statsmodels/tsa/arima_model.py
--- a/statsmodels/tsa/arima_model.py
+++ b/statsmodels/tsa/arima_model.py
@@ -399,6 +399,13 @@ def _fit_start_params_hr(self, order):
                 armod = AR(endog).fit(ic='bic', trend='nc')
                 arcoefs_tmp = armod.params
                 p_tmp = armod.k_ar
+                # it's possible in small samples that optimal lag-order
+                # doesn't leave enough obs. No consistent way to fix.
+                if p_tmp + q >= len(endog):
+                    raise ValueError("Proper starting parameters cannot"
+                            " be found for this order with this number "
+                            "of observations. Use the start_params "
+                            "argument.")
                 resid = endog[p_tmp:] - np.dot(lagmat(endog, p_tmp,
                                 trim='both'), arcoefs_tmp)
                 if p < p_tmp + q:
diff --git a/statsmodels/tsa/vector_ar/util.py b/statsmodels/tsa/vector_ar/util.py
--- a/statsmodels/tsa/vector_ar/util.py
+++ b/statsmodels/tsa/vector_ar/util.py
@@ -60,6 +60,8 @@ def make_lag_names(names, lag_order, trendorder=1):
     # take care of lagged endogenous names
     for i in range(1, lag_order + 1):
         for name in names:
+            if not isinstance(name, basestring):
+                name = str(name) # will need consistent unicode handling
             lag_names.append('L'+str(i)+'.'+name)
 
     # handle the constant name


Test patch:
diff --git a/statsmodels/tsa/tests/test_arima.py b/statsmodels/tsa/tests/test_arima.py
--- a/statsmodels/tsa/tests/test_arima.py
+++ b/statsmodels/tsa/tests/test_arima.py
@@ -1796,6 +1796,32 @@ def test_bad_start_params():
     arima_mod = ARIMA(np.log(inv), (1,1,2))
     assert_raises(ValueError, mod.fit)
 
+def test_arima_small_data_bug():
+    # Issue 1038, too few observations with given order
+    from datetime import datetime
+    import statsmodels.api as sm
+
+    vals = [96.2, 98.3, 99.1, 95.5, 94.0, 87.1, 87.9, 86.7402777504474]
+
+    dr = dates_from_range("1990q1", length=len(vals))
+    ts = pandas.TimeSeries(vals, index=dr)
+    df = pandas.DataFrame(ts)
+    mod = sm.tsa.ARIMA(df, (2, 0, 2))
+    assert_raises(ValueError, mod.fit)
+
+def test_arima_dataframe_integer_name():
+    # Smoke Test for Issue 1038
+    from datetime import datetime
+    import statsmodels.api as sm
+
+    vals = [96.2, 98.3, 99.1, 95.5, 94.0, 87.1, 87.9, 86.7402777504474,
+            94.0, 96.5, 93.3, 97.5, 96.3, 92.]
+
+    dr = dates_from_range("1990q1", length=len(vals))
+    ts = pandas.TimeSeries(vals, index=dr)
+    df = pandas.DataFrame(ts)
+    mod = sm.tsa.ARIMA(df, (2, 0, 2))
+
 if __name__ == "__main__":
     import nose
     nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb'], exit=False)