title: Python - Merge data by time intervals (R data.table mimic?)
body: <p>I'm just learning python and have a question about integrating data frames by time. For instance, say I have 2 separate data frames with irregular time intervals but grouped by study_id. I would like to join rows that come within 2 hours of each other. </p>
  
  <p>Previously, I have used the data.table package in R for this. An example of this code is below. </p>
  
  <pre><code>df_new &lt;- df1[df2, on="Study_ID", allow.cartesian=T][difftime(`date_df1`, `date_df2`, units="hours") &lt;= 2 &amp; difftime(`date_df1`, `date_df2`, units="hours") &gt;= - 2] 
  </code></pre>
  
  <p>This code then binds every instance where there are dates that are within 2 hours of each for each data frame. I am looking to see if there is any similar code for python? Ideally, I'd like to merge these rows so that I can find the max value that occurs between measurements that come either within 2 hours before or after the measurements.</p>
  
  <p>Any thoughts? Thank you!</p>
  
  <p>Edit: Example of Dataframes</p>
  
  <pre><code>    ID   Date           HeartRate
      1    4/1/2019 04:13     56
      1    4/2/2019 05:30     45
      1    4/3/2019 22:10     61
      2    4/3/2019 23:13     62
      2    4/5/2019 15:10     67
  
      df2
      ID   Date             Weight
       1    4/1/2019 06:10     112
       1    4/2/2019 02:30     114
       1    4/3/2019 21:10     112.5
       2    4/3/2019 23:10     113
       2    4/4/2019 00:00     114
  
      Output (this is what I would love!)
      ID   Date(blood pressure)  HeartRate   Date(weight)   Weight
      1    4/1/2019 4:13            56       4/1/2019 06:10   112
      1    4/3/2019 22:10           61       4/3/2019 21:10   112.5
      2    4/3/2019 23:13           62       4/3/2019 23:10   113
      2    4/3/2019 23:13           62       4/4/2019 00:00   114
  </code></pre>
  
  <p>In this example, the second row in each dateframe is just removed since these measurements don' thave a pair within 2 hours. But the second to last row shown in df1 repeats because it has 2 cases in df2 that are within 2 hours.</p>