Calculate local time derivative of Series
<p>I have data that I'm importing from an hdf5 file.  So, it comes in looking like this:</p>

<pre><code>import pandas as pd
tmp=pd.Series([1.,3.,4.,3.,5.],['2016-06-27 23:52:00','2016-06-27 23:53:00','2016-06-27 23:54:00','2016-06-27 23:55:00','2016-06-27 23:59:00'])
tmp.index=pd.to_datetime(tmp.index)

&gt;&gt;&gt;tmp
2016-06-27 23:52:00    1.0
2016-06-27 23:53:00    3.0
2016-06-27 23:54:00    4.0
2016-06-27 23:55:00    3.0
2016-06-27 23:59:00    5.0
dtype: float64
</code></pre>

<p>I would like to find the local slope of the data.  If I just do tmp.diff() I do get the local change in value.  But, I want to get the change in value per second (time derivative)
I would like to do something like this, but this is the wrong way to do it and gives an error:</p>

<pre><code>tmp.diff()/tmp.index.diff()
</code></pre>

<p>I have figured out that I can do it by converting all the data to a DataFrame, but that seems inefficient.  Especially, since I'm going to have to work with a large, on disk file in chunks.
Is there a better way to do it other than this:</p>

<pre><code>df=pd.DataFrame(tmp)
df['secvalue']=df.index.astype(np.int64)/1e+9
df['slope']=df['Value'].diff()/df['secvalue'].diff()
</code></pre>

<p>Use <code>numpy.gradient</code></p>

<pre><code>import numpy as np
import pandas as pd

slope = pd.Series(np.gradient(tmp.values), tmp.index, name='slope')
</code></pre>

<p>To address the unequal temporal index, i'd resample over minutes and interpolate.  Then my gradients would be over equal intervals.</p>

<pre><code>tmp_ = tmp.resample('T').interpolate()

slope = pd.Series(np.gradient(tmp_.values), tmp_.index, name='slope')

df = pd.concat([tmp_.rename('values'), slope], axis=1)
df
</code></pre>

<p><a href="http://i.stack.imgur.com/SCX6d.png" rel="nofollow"><img src="http://i.stack.imgur.com/SCX6d.png" alt="enter image description here"></a></p>

<pre><code>df.plot()
</code></pre>

<p><a href="http://i.stack.imgur.com/HQRC0.png" rel="nofollow"><img src="http://i.stack.imgur.com/HQRC0.png" alt="enter image description here"></a></p>

python datetime pandas