Calculating the time difference between events
<p>I have a df</p>

<pre><code>df = pd.DataFrame({'State': {0: "A", 1: "B", 2:"A", 3: "B", 4: "A", 5: "B", 6 : "A", 7: "B"}, 
               'date': {0: '2016-10-13T14:10:41Z', 1: '2016-10-13T14:10:41Z', 2:'2016-10-13T15:26:19Z',
                        3: '2016-10-14T15:26:19Z', 4: '2016-10-15T15:26:19Z', 5: '2016-10-18T15:26:19Z',
                        6 :'2016-10-17T15:26:19Z', 7: '2016-10-13T15:26:19Z'}}, columns=['State', 'date'])
</code></pre>

<p>I need to get an average of the time between each a event and the following b event. I'm trying to use shift to generate a series of differences to average it but I can't quite get it to work.</p>

<p>Thank you!</p>

<p>First, convert the dates to datetimes, then use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.diff.html" rel="nofollow"><code>DataFrame.diff</code></a>:</p>

<pre><code>df.date = pd.to_datetime(df.date)
df.date.diff()
</code></pre>

<p>yields:</p>

<pre><code>0                 NaT
1     0 days 00:00:00
2     0 days 01:15:38
3     1 days 00:00:00
4     1 days 00:00:00
5     3 days 00:00:00
6   -1 days +00:00:00
7   -4 days +00:00:00
Name: date, dtype: timedelta64[ns]
</code></pre>

<p>If you want the average, you can do something like</p>

<pre><code>df.date.diff().mean() # or possibly df.date.diff().abs().mean()
# Timedelta('0 days 00:10:48.285714')
</code></pre>

python pandas