title: how to speed up joining large datasets on nonequal time values
body: <p>I have 2 large dataframes <code>df1</code> and <code>df2</code>, both of which have a column <code>time</code>. I'd like to join these 2 tables. However, there might not always be an exact match. In this case, I'd like to join such that I take the latest time value in <code>df2</code> that occurred before the time value for <code>df1</code> in an efficient manner.</p>
  <p>For example, given the tables</p>
  <pre class="lang-py prettyprint-override"><code>df1
  Time             | val_1
  ------------------------
  1/1/1980 1:00:00 | 1
  1/1/1980 2:00:00 | 2
  1/1/1980 3:00:00 | 3
  1/1/1980 4:00:00 | 4
  
  df2
  time             | val_2
  ------------------------
  1/1/1980 1:00:00 | 5
  1/1/1980 1:59:59 | 6
  1/1/1980 3:00:01 | 7
  1/1/1980 3:30:30 | 8
  </code></pre>
  <p>The final table should be</p>
  <pre class="lang-py prettyprint-override"><code>time             | val_1 | val_2
  --------------------------------
  1/1/1980 1:00:00 | 1     | 5
  1/1/1980 2:00:00 | 2     | 6
  1/1/1980 3:00:00 | 3     | 6
  1/1/1980 4:00:00 | 4     | 8
  
  </code></pre>
  <p>I'm currently doing this, but the runtime is too high</p>
  <pre class="lang-py prettyprint-override"><code>def prevrow(t):
      return df2.iloc[df2['time'].apply(lambda x: t - x if t &gt;= x else np.nan).idxmin()]
  pd.concat([df1,df1['Time'].apply(prevrow)], axis=1)
  </code></pre>
  <p><strong>How would I speed this up?</strong></p>