title: Pandas merge depending on timestamp
body: <p>I have two dataframes, where the first <code>df1</code> contains sales data like</p>
  
  <pre><code>Item    Revenue    Date
  ring    200        2018-05-21
  led     51         2018-09-18
  ring    600        2018-12-25
  </code></pre>
  
  <p>and the second <code>df2</code> contains sellers assignation for every item. Note that assignation may change in time and the history is preserved</p>
  
  <pre><code>Item    Seller    Since_when
  led     Mike      2018-01-01
  ring    John      2018-01-01
  ring    Mike      2018-12-01
  </code></pre>
  
  <p>The idea is to merge both dataframes on <code>Item</code> by a date rule so the sale is assigned to the seller who actually sold the item in that moment, resulting</p>
  
  <pre><code>Item    Revenue    Date          Seller
  ring    200        2018-05-21    John
  led     51         2018-09-18    Mike
  ring    600        2018-12-25    Mike (Mike started selling rings on December)
  </code></pre>
  
  <p>Is there a way to tell <code>pd.merge</code> to take the newest row of the second dataframe?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the code:</p>
  
  <pre><code>import pandas as pd
  
  m1 = pd.DataFrame([['ring', 200, 2018, 5, 21], ['led', 51, 2018, 9, 18], ['ring', 600, 2018, 12, 25]])
  m1.columns = ['item', 'revenue', 'año', 'mes', 'dia']
  
  m1['date'] = m1['año'].astype(str) + '-' + m1['mes'].astype(str) + '-' + m1['dia'].astype(str)
  
  m2 = pd.DataFrame([['led', 'Mike', '2018-01-01'], ['ring', 'John', '2018-01-01'], ['ring', 'Mike', '2018-12-01']])
  m2.columns = ['item', 'persona', 'fecha']
  
  m = (pd.merge_asof(m1.sort_values('date'), m2.sort_values('fecha'),
                     by='item', left_on='date', right_on='fecha')
         .drop(columns='fecha'))
  </code></pre>