title: Statsmodel sandwich "all groups are empty taking lags" error when using HAC-Panel clustered standard errors
body: <p>I have data that looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">account</th>
  <th style="text-align: center;">DV</th>
  <th style="text-align: center;">month</th>
  <th style="text-align: center;">year</th>
  <th style="text-align: center;">yearmonth</th>
  <th style="text-align: center;">pre/post</th>
  <th style="text-align: center;">group</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">1</td>
  <td style="text-align: center;">121</td>
  <td style="text-align: center;">oct</td>
  <td style="text-align: center;">2019</td>
  <td style="text-align: center;">oct 2019</td>
  <td style="text-align: center;">pre</td>
  <td style="text-align: center;">control</td>
  </tr>
  <tr>
  <td style="text-align: center;">1</td>
  <td style="text-align: center;">124</td>
  <td style="text-align: center;">Nov</td>
  <td style="text-align: center;">2019</td>
  <td style="text-align: center;">nov 2019</td>
  <td style="text-align: center;">post</td>
  <td style="text-align: center;">control</td>
  </tr>
  <tr>
  <td style="text-align: center;">2</td>
  <td style="text-align: center;">120</td>
  <td style="text-align: center;">oct</td>
  <td style="text-align: center;">2019</td>
  <td style="text-align: center;">oct 2019</td>
  <td style="text-align: center;">pre</td>
  <td style="text-align: center;">treatment</td>
  </tr>
  <tr>
  <td style="text-align: center;">2</td>
  <td style="text-align: center;">118</td>
  <td style="text-align: center;">nov</td>
  <td style="text-align: center;">2019</td>
  <td style="text-align: center;">nov 2019</td>
  <td style="text-align: center;">post</td>
  <td style="text-align: center;">treatment</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I run a difference-in-difference regression:</p>
  <pre><code>results2 = smf.ols(&quot;DV ~ C(group, Treatment('control')) * C(pre_period, Treatment(True)) + month + C(year)&quot;,
                    df99).fit(cov_type='HAC-Panel', cov_kwds={'groups':df99['account'], 'time':df99['yearmonth'], 'maxlags':35})
  print(results2.summary())
  </code></pre>
  <p>And I get the error message below.
  I do the same thing with a different dataset that is more or less the same (different experiment) but do not encounter the problem. My data cleaning process is essentially identical. Furthermore, just a few days ago, this was working fine. But it has now suddenly thrown up this error (I did reverse any changes I had made).</p>
  <p>I can't make sense of this error at all. Even searching for this error message in &quot;sandwich_covariance.py&quot; doesn't reveal anything.</p>
  <p>This person has had a similar problem but no solution was proposed: <a href="https://stackoverflow.com/questions/61465483/python-statsmodels-robust-cov-type-hac-panel-issue">Python statsmodels robust cov_type=&#39;hac-panel&#39; issue</a></p>
  <pre><code>  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-43-0dcbaef1325b&gt; in &lt;module&gt;
  ----&gt; 1 results2 = smf.ols(&quot;dv ~ C(group, Treatment('control')) * C(pre_period, Treatment(True)) + month + C(year)&quot;,
        2                   df99).fit(cov_type='HAC-Panel', cov_kwds={'groups':df99['account'], 'time':df99['yearmonth'], 'maxlags':35})
        3 print(results2.summary())
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/regression/linear_model.py in fit(self, method, cov_type, cov_kwds, use_t, **kwargs)
      340 
      341         if isinstance(self, OLS):
  --&gt; 342             lfit = OLSResults(
      343                 self, beta,
      344                 normalized_cov_params=self.normalized_cov_params,
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/regression/linear_model.py in __init__(self, model, params, normalized_cov_params, scale, cov_type, cov_kwds, use_t, **kwargs)
     1584                     use_t = use_t_2
     1585                 # TODO: warn or not?
  -&gt; 1586             self.get_robustcov_results(cov_type=cov_type, use_self=True,
     1587                                        use_t=use_t, **cov_kwds)
     1588         for key in kwargs:
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/regression/linear_model.py in get_robustcov_results(self, cov_type, use_t, **kwargs)
     2530             groupidx = lzip([0] + tt, tt + [nobs_])
     2531             self.n_groups = n_groups = len(groupidx)
  -&gt; 2532             res.cov_params_default = sw.cov_nw_panel(self, maxlags, groupidx,
     2533                                                      weights_func=weights_func,
     2534                                                      use_correction=use_correction)
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/stats/sandwich_covariance.py in cov_nw_panel(results, nlags, groupidx, weights_func, use_correction)
      785     xu, hessian_inv = _get_sandwich_arrays(results)
      786 
  --&gt; 787     S_hac = S_nw_panel(xu, weights, groupidx)
      788     cov_hac = _HCCM2(hessian_inv, S_hac)
      789     if use_correction:
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/stats/sandwich_covariance.py in S_nw_panel(xw, weights, groupidx)
      723     S = weights[0] * np.dot(xw.T, xw)  #weights just for completeness
      724     for lag in range(1, nlags+1):
  --&gt; 725         xw0, xwlag = lagged_groups(xw, lag, groupidx)
      726         s = np.dot(xw0.T, xwlag)
      727         S += weights[lag] * (s + s.T)
  
  ~/opt/anaconda3/envs/pyr/lib/python3.8/site-packages/statsmodels/stats/sandwich_covariance.py in lagged_groups(x, lag, groupidx)
      706 
      707     if out0 == []:
  --&gt; 708         raise ValueError('all groups are empty taking lags')
      709     #return out0, out_lagged
      710     return np.vstack(out0), np.vstack(out_lagged)
  
  ValueError: all groups are empty taking lags
  </code></pre>