title: pandas - finding most recent (but previous) date in a second reference dataframe
body: <p>I have two dataframes and for one I want to find the closest (previous) date in the other.</p>
  
  <p><strong>If the date matches then I need to take the previous date</strong></p>
  
  <p><code>df_main</code> contains the reference information</p>
  
  <p>For <code>df_sample</code> I want to lookup the <code>Time</code> in <code>df_main</code> for the closest (but previous) entry. I can do this using <code>method='ffill'</code> , but where the date for the <code>Time</code> field is the same day it returns that day - I want it to return the previous - basically a <code>&lt;</code> rather than <code>&lt;=</code>.</p>
  
  <p>In my example <code>df_res</code> I want the <code>closest_val</code> column to contain <code>[ "n/a", 90, 90, 280, 280, 280]</code></p>
  
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  
  dsample = {'Index': [1, 2, 3, 4, 5, 6],
       'Time': ["2020-06-01", "2020-06-02", "2020-06-03", "2020-06-04" ,"2020-06-05" ,"2020-06-06"],
       'Pred': [100, -200, 300, -400 , -500, 600]
      }
  
  dmain = {'Index': [1, 2, 3],
       'Time': ["2020-06-01", "2020-06-03","2020-06-06"],
       'Actual': [90, 280, 650]
      }
  
  def find_closest(x, df2):
      df_res = df2.iloc[df2.index.get_loc(x['Time'], method='ffill')]
      x['closest_time'] = df_res['Time']
      x['closest_val'] = df_res['Actual']
      return x
  
  df_sample = pd.DataFrame(data=dsample)
  df_main = pd.DataFrame(data=dmain)
  
  df_sample = df_sample.set_index(pd.DatetimeIndex(df_sample['Time']))
  df_main = df_main.set_index(pd.DatetimeIndex(df_main['Time']))
  df_res = df_sample.apply(find_closest, df2=df_main ,axis=1)
  
  
  
  </code></pre>
  
  <p><img src="https://i.ibb.co/q1pW8mj/Screenshot-from-2020-06-16-11-50-53.png" alt="tables"></p>