Pandas - Calculating daily differences relative to earliest value
<p>This is probably pretty easy, but for some reason I am finding it quite difficult to complete. Any tips would be greatly appreciated. I have some time series data consisting of 5-minute intervals each day, ala:</p>

<pre><code>Date                   Values
2012-12-05 09:30:00    5
2012-12-05 09:35:00    7
2012-12-05 09:40:00    3
2012-12-05 09:45:00    2
2012-12-05 09:50:00    15
2012-12-06 09:30:00    4
2012-12-06 09:35:00    3
2012-12-06 09:40:00    8
2012-12-06 09:45:00    1
</code></pre>

<p>I would like to calculate the differences relative to the first value of the day (which in this case always will be the 9:30 value), ie. end up with this DataFrame:</p>

<pre><code>Date                   Values
2012-12-05 09:30:00    0
2012-12-05 09:35:00    2
2012-12-05 09:40:00    -2
2012-12-05 09:45:00    -3
2012-12-05 09:50:00    10
2012-12-06 09:30:00    0
2012-12-06 09:35:00    -1
2012-12-06 09:40:00    4
2012-12-06 09:45:00    -3
</code></pre>

<p>You need substract by <code>Series</code> created <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.transform.html" rel="nofollow"><code>transform</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.date.html" rel="nofollow"><code>Series.dt.date</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.first.html" rel="nofollow"><code>first</code></a>:</p>

<pre><code>print (df.Values.groupby(df.Date.dt.day).transform('first'))
0    5
1    5
2    5
3    5
4    5
5    4
6    4
7    4
8    4
Name: Values, dtype: int64

df.Values = df.Values - df.Values.groupby(df.Date.dt.day).transform('first')

print (df)
                 Date  Values
0 2012-12-05 09:30:00       0
1 2012-12-05 09:35:00       2
2 2012-12-05 09:40:00      -2
3 2012-12-05 09:45:00      -3
4 2012-12-05 09:50:00      10
5 2012-12-06 09:30:00       0
6 2012-12-06 09:35:00      -1
7 2012-12-06 09:40:00       4
8 2012-12-06 09:45:00      -3
</code></pre>

python pandas resampling