title: Panel data regression with fixed effects using Python
body: <p>I have the following panel stored in <code>df</code>:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: right;"></th>
  <th style="text-align: right;">state</th>
  <th style="text-align: right;">district</th>
  <th style="text-align: right;">year</th>
  <th style="text-align: right;">y</th>
  <th style="text-align: right;">constant</th>
  <th style="text-align: right;">x1</th>
  <th style="text-align: right;">x2</th>
  <th style="text-align: right;">time</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: right;">0</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01001</td>
  <td style="text-align: right;">2009</td>
  <td style="text-align: right;">12</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.956007</td>
  <td style="text-align: right;">639673</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01001</td>
  <td style="text-align: right;">2010</td>
  <td style="text-align: right;">20</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.972175</td>
  <td style="text-align: right;">639673</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: right;">2</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01001</td>
  <td style="text-align: right;">2011</td>
  <td style="text-align: right;">22</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.988343</td>
  <td style="text-align: right;">639673</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01002</td>
  <td style="text-align: right;">2009</td>
  <td style="text-align: right;">0</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0</td>
  <td style="text-align: right;">33746</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: right;">4</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01002</td>
  <td style="text-align: right;">2010</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.225071</td>
  <td style="text-align: right;">33746</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01002</td>
  <td style="text-align: right;">2011</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.450142</td>
  <td style="text-align: right;">33746</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: right;">6</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01003</td>
  <td style="text-align: right;">2009</td>
  <td style="text-align: right;">0</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0</td>
  <td style="text-align: right;">45196</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: right;">7</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01003</td>
  <td style="text-align: right;">2010</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.427477</td>
  <td style="text-align: right;">45196</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: right;">8</td>
  <td style="text-align: right;">01</td>
  <td style="text-align: right;">01003</td>
  <td style="text-align: right;">2011</td>
  <td style="text-align: right;">9</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: right;">0.854955</td>
  <td style="text-align: right;">45196</td>
  <td style="text-align: right;">3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <ul>
  <li><code>y</code> is the number of protests in each district</li>
  <li><code>constant</code> is a column full of ones</li>
  <li><code>x1</code> is the proportion of the district's area covered by a mobile network provider</li>
  <li><code>x2</code> is the population count in each district (note that it is fixed in time)</li>
  </ul>
  <p>How can I run the following model in Python?
  <a href="https://i.stack.imgur.com/vq6El.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vq6El.png" alt="model" /></a></p>
  <hr />
  <p>Here's what I tried</p>
  <pre class="lang-py prettyprint-override"><code># Transform `x2` to match model
  df['x2'] = df['x2'].multiply(df['time'], axis=0)
  # District fixed effects
  df['delta'] = pd.Categorical(df['district'])
  # State-time fixed effects
  df['eta'] = pd.Categorical(df['state'] + df['year'].astype(str))
  # Set indexes
  df.set_index(['district','year'])
  
  from linearmodels.panel import PanelOLS
  m = PanelOLS(dependent=df['y'], exog=df[['constant','x1','x2','delta','eta']])
  </code></pre>
  <blockquote>
  <p>ValueError: exog does not have full column rank. If you wish to proceed with model estimation irrespective of the numerical accuracy of coefficient estimates, you can set rank_check=False.</p>
  </blockquote>
  <p>What am I doing wrong?</p>