Pandas Dataframe comparison with range/stdev
<p>I have to dataframes A and B. Both have a 'datetime' column and another common column X. B is bigger as it has X at every minute while A only has X at intermittent times during a day. For each 'datetime' in A, I want to calculate the range (or stdev) of X during previous 30 minutes using B. So something like:</p>

<p><code>For i,time in enumerate(A['time']): 
   temp=B[(B['time'] &gt; time -timedelta(days=0, minutes=30)) and (B['time'] &lt;=time]
   range[i]=temp.max-temp.min</code></p>

<p>Here temp is intended to snapshot all the data within 30 mins prior to A's timestamp in B. Is there a simple way, maybe one that doesn't require a loop, to do this? Note that sizes of A and B are different.</p>

<p>Sample A: (only few outputs available along with inputs)
<code>
Date                         input  output 
2015-01-02 20:48:00-00:00     1120    343 
2015-01-02 21:25:00-00:00     1345    365</code></p>

<p>Sample B: ( all the inputs available but no outputs)
<code>
Date                         input 
2014-11-02 00:32:00-00:00     1542
2014-11-02 00:33:00-00:00     1285</code></p>

<p>Sample Output:
<code>
Date                         input  output  Range_input_previous 30 min 
2015-01-02 20:48:00-00:00     1120    343      ??(use table B)
2015-01-02 21:25:00-00:00     1345    365      ??(use table B)</code></p>

<p>This works for me but uses a loop:</p>

<p><code>rangex=np.zeros(A.shape[0])<br>
 for i,Date in enumerate(A['Date']):
     temp=B[(B['Date'] &gt; Date-datetime.timedelta(days=0, minutes=30)) &amp; (B['Date'] &lt;=Date)]
     rangex[i]=temp['input'].max()-temp['input'].min()<br>
 A['range']=pd.Series(rangex, index=A.index)</code></p>

python pandas dataframe timestamp range