Repository: statsmodels/statsmodels

Issue:
BUG: `attach_rows` does not work when Series change index
#### Describe the bug

When you set the index in a Series constructor with different values it looks up rather than assigns the series.

See https://stackoverflow.com/questions/66523923/how-to-get-residuals-from-statsmodels-autoregresults-model-resid-returns-all-na/66578922?noredirect=1#comment117752130_66578922


#### Code Sample, a copy-pastable example if possible

See https://stackoverflow.com/questions/66523923/how-to-get-residuals-from-statsmodels-autoregresults-model-resid-returns-all-na/66578922?noredirect=1#comment117752130_66578922

<details>

**Note**: As you can see, there are many issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates.

**Note**: Please be sure you are using the latest released version of `statsmodels`, or a recent build of `master`. If your problem has been fixed in an unreleased version, you might be able to use `master` until a new release occurs. 

**Note**: If you are using a released version, have you verified that the bug exists in the master branch of this repository? It helps the limited resources if we know problems exist in the current master so that they do not need to check whether the code sample produces a bug in the next release.

</details>


If the issue has not been resolved, please file it in the issue tracker.

#### Expected Output

Not all `nan` values.

#### Output of ``import statsmodels.api as sm; sm.show_versions()``

<details>

[paste the output of ``import statsmodels.api as sm; sm.show_versions()`` here below this line]

</details>



Discussion:

Resolving patch:
diff --git a/statsmodels/base/data.py b/statsmodels/base/data.py
--- a/statsmodels/base/data.py
+++ b/statsmodels/base/data.py
@@ -578,10 +578,12 @@ def attach_rows(self, result):
             squeezed = squeezed[None, :]
         # May be zero-dim, for example in the case of forecast one step in tsa
         if squeezed.ndim < 2:
-            return Series(squeezed, index=self.row_labels[-len(result):])
+            out = Series(squeezed)
         else:
-            return DataFrame(result, index=self.row_labels[-len(result):],
-                             columns=self.ynames)
+            out = DataFrame(result)
+            out.columns = self.ynames
+        out.index = self.row_labels[-len(result):]
+        return out
 
     def attach_dates(self, result):
         squeezed = result.squeeze()


Test patch:
diff --git a/statsmodels/tsa/tests/test_ar.py b/statsmodels/tsa/tests/test_ar.py
--- a/statsmodels/tsa/tests/test_ar.py
+++ b/statsmodels/tsa/tests/test_ar.py
@@ -14,6 +14,7 @@
 import statsmodels.api as sm
 from statsmodels.iolib.summary import Summary
 from statsmodels.regression.linear_model import OLS
+from statsmodels.tools.sm_exceptions import ValueWarning
 from statsmodels.tools.testing import assert_equal
 from statsmodels.tools.tools import Bunch
 from statsmodels.tsa.ar_model import AR, AutoReg, ar_select_order
@@ -1553,3 +1554,18 @@ def test_autoreg_plot_err():
     res = mod.fit()
     with pytest.raises(ValueError):
         res.plot_predict(0, end=50, in_sample=False)
+
+
+def test_autoreg_resids():
+    idx = pd.date_range("1900-01-01", periods=250, freq="M")
+    rs = np.random.RandomState(0)
+    idx_dates = sorted(rs.choice(idx, size=100, replace=False))
+    e = rs.standard_normal(250)
+    y = np.zeros(250)
+    y[:2] = e[:2]
+    for i in range(2, 250):
+        y[i] = 2 + 1.8 * y[i - 1] - 0.95 * y[i - 2] + e[i]
+    ys = pd.Series(y[-100:], index=idx_dates, name="y")
+    with pytest.warns(ValueWarning):
+        res = AutoReg(ys, lags=2, old_names=False).fit()
+    assert np.all(np.isfinite(res.resid))