title: Trim Group by Column/Series Sequence in Pandas by NaN Ocurrence
body: <p>I have a data frame as follows:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>user_id</th>
  <th>metric_date</th>
  <th>metric_val1</th>
  <th>is_churn</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>3</td>
  <td>2021-01</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-02</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-03</td>
  <td>0.4</td>
  <td>False</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-04</td>
  <td>0.5</td>
  <td>False</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-05</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-01</td>
  <td>0.1</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-02</td>
  <td>0.3</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-03</td>
  <td>0.2</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-04</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-05</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Suppose there are other metric columns, but the main reference is metric_val1, how can I grouping by <strong>user_id</strong> and trim all row that have NaN value before the first valid metric_val1, and keeping only the last NaN after the last valid value for metric_val1, the output should be something like that (Assume that there is no gap in the valid values) :</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>user_id</th>
  <th>metric_date</th>
  <th>metric_val1</th>
  <th>is_churn</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>3</td>
  <td>2021-03</td>
  <td>0.4</td>
  <td>False</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-04</td>
  <td>0.5</td>
  <td>False</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2021-05</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-01</td>
  <td>0.1</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-02</td>
  <td>0.3</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-03</td>
  <td>0.2</td>
  <td>False</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2021-04</td>
  <td>NaN</td>
  <td>True</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Can someone help me with an efficient way to do that in pandas?</p>