title: Determing records with equivalent date columns
body: <p>I've got an input file like so,</p>
  <pre><code>GROUP_ID,CLASS_ID,A_TIME,B_TIME,ORDR_ID
  123,456,2022/07/11 22:00:00,2022/07/12 22:30:00,huhu
  123,456,2022/07/11 22:00:00,2022/07/10 22:00:00,hjao
  321,654,2022/07/11 22:00:00,2022/07/11 12:10:00,mjhu
  321,654,2022/07/11 22:00:00,2022/07/11 21:10:00,uqun
  321,654,2022/07/11 22:00:00,2022/07/11 22:10:00,ibyy
  666,555,2022/07/11 22:00:00,2022/07/12 22:00:00,oqih
  666,555,2022/07/11 22:00:00,2022/07/10 22:00:00,ocbd
  </code></pre>
  <p>With respect to each <code>GROUP_ID,CLASS_ID</code>, I'm trying to obtain the record with <code>B_TIME</code> nearest to <code>A_TIME</code>. This is what I've done so far:</p>
  <pre><code>df = pd.read_csv(&quot;JoinInput.txt&quot;, dtype=str, parse_dates=['A_TIME','B_TIME'])
  pdf = df.loc[
      df.assign(mins=(df[&quot;A_TIME&quot;] - df[&quot;B_TIME&quot;]))
          .groupby([&quot;CLASS_ID&quot;,&quot;GROUP_ID&quot;])[&quot;mins&quot;]
          .idxmin()]
  print(pdf)
  </code></pre>
  <p>But I'm trying to handle 2 special cases, which is where I'm stuck:</p>
  <p>1.In case when multiple records are present for that <code>GROUP_ID,CLASS_ID</code>(666,555 in above example) with same <code>mins</code>, then need to consider the record with B_TIME &gt; A_TIME.</p>
  <p>2.In case when multiple records are present for that <code>GROUP_ID,CLASS_ID</code> (123,456 in above example) with different <code>mins</code>, then need to consider the record with B_TIME nearest to A_TIME irrespective of whether B_TIME comes before/after A_TIME.</p>
  <pre><code>Expected Output:
  GROUP_ID,CLASS_ID,A_TIME,B_TIME,ORDR_ID
  123,456,2022/07/11 22:00:00,2022/07/10 22:00:00,hjao
  321,654,2022/07/11 22:00:00,2022/07/11 22:10:00,ibyy
  666,555,2022/07/11 22:00:00,2022/07/12 22:00:00,oqih
  </code></pre>
  <p>Would appreciate some suggestions on handling above cases.</p>