Pandas Calculating minimum timedelta by group
<p>My input data frame as below:
Input Dataframe:</p>

<pre><code>Input1 = pd.DataFrame({'LOT': {0: 'A1', 1: 'A2', 2: 'A3', 3: 'A4', 4: 'A5'},
 'OPERATION': {0: 100.0, 1: 100.0, 2: 100.0, 3: 100.0, 4: 100.0},
 'TXN_DATE': {0: '12/6/2016',
  1: '12/5/2016',
  2: '11/30/2016',
  3: '11/27/2016',
  4: '11/22/2016'}})

Input2 = pd.DataFrame({'LOT': {0: 'B1', 1: 'B2', 2: 'B3', 3: 'B4', 4: 'B5', 5: 'B6'},
 'OPERATION': {0: 500, 1: 500, 2: 500, 3: 500, 4: 500, 5: 500},
 'TXN_DATE': {0: '12/7/2016',
  1: '12/3/2016',
  2: '11/17/2016',
  3: '11/22/2016',
  4: '12/4/2016',
  5: '12/3/2016'}})
</code></pre>

<p>I am interesting to calculate companion lot from Input2 to lot in Input1 table based on minimum TXN_DATES delta between them (time delta suppose to be minimal):</p>

<p>Final DataFrame:</p>

<pre><code>Expected_out =  pd.DataFrame({'COMPANION_LOT': {0: 'B5', 1: 'B5', 2: 'B4', 3: 'B4', 4: 'B4'},
 'COMPANION_LOT TXN_DATE': {0: '12/4/2016',
  1: '12/4/2016',
  2: '11/22/2016',
  3: '11/22/2016',
  4: '11/22/2016'},
 'LOT': {0: 'A1', 1: 'A2', 2: 'A3', 3: 'A4', 4: 'A5'},
 'OPERATION': {0: 100, 1: 100, 2: 100, 3: 100, 4: 100},
 'TXN_DATE': {0: '12/6/2016',
  1: '12/5/2016',
  2: '11/30/2016',
  3: '11/27/2016',
  4: '11/22/2016'}})`
</code></pre>

<p>Thank you</p>