title: Keep the row immediately after a date (Pandas)
body: <p>I have a DataFrame which looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Record Date</th>
  <th>Cutoff Date</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A</td>
  <td>2022-01-01</td>
  <td>2022-01-31</td>
  <td>1</td>
  </tr>
  <tr>
  <td>A</td>
  <td>2022-01-15</td>
  <td>2022-01-31</td>
  <td>2</td>
  </tr>
  <tr>
  <td>A</td>
  <td>2022-01-28</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>B</td>
  <td>2022-01-15</td>
  <td>2022-01-31</td>
  <td>1</td>
  </tr>
  <tr>
  <td>B</td>
  <td>2022-01-28</td>
  <td>2022-01-31</td>
  <td>2</td>
  </tr>
  <tr>
  <td>B</td>
  <td>2022-01-31</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>C</td>
  <td>2022-01-01</td>
  <td>2022-01-31</td>
  <td>1</td>
  </tr>
  <tr>
  <td>C</td>
  <td>2022-01-27</td>
  <td>2022-01-31</td>
  <td>2</td>
  </tr>
  <tr>
  <td>C</td>
  <td>2022-02-01</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>C</td>
  <td>2022-02-03</td>
  <td>2022-01-31</td>
  <td>4</td>
  </tr>
  <tr>
  <td>D</td>
  <td>2022-06-03</td>
  <td>2022-01-31</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>For each <code>ID</code>, I want to keep only 1 row, based on the following criteria:</p>
  <ol>
  <li>If there is a row with <code>Record Date</code> == <code>Cutoff Date</code>, keep this row.</li>
  <li>If all <code>Record Date</code> is before <code>Cutoff Date</code>, keep the row which is closet to <code>Cutoff Date</code>.</li>
  <li>If there exist any <code>Record Date</code> after <code>Cutoff Date</code>, keep the earliest row after <code>Cutoff Date</code>.</li>
  <li>If there is only 1 row for an <code>ID</code>, keep this row.</li>
  </ol>
  <p>Note that <code>Cutoff Date</code> is constant for each <code>ID</code>. (I set all <code>Cutoff Date</code> identical to make it simplier).</p>
  <p>The desired output:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Record Date</th>
  <th>Cutoff Date</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A</td>
  <td>2022-01-28</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>B</td>
  <td>2022-01-31</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>C</td>
  <td>2022-02-01</td>
  <td>2022-01-31</td>
  <td>3</td>
  </tr>
  <tr>
  <td>D</td>
  <td>2022-06-03</td>
  <td>2022-01-31</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>