pandas map and timedelta with missing values
<p>Suppose I have the following dataframe (df)</p>

<p><img src="https://i.stack.imgur.com/ehNsc.png" alt="dataframe without time_diff"></p>

<p>I want to create a column that reports the difference between successive Timestamps for each ID, which is straightforward:</p>

<pre><code>df['time_diff'] = df.groupby('ID')['Timestamp'].diff()
</code></pre>

<p>which yields</p>

<p><img src="https://i.stack.imgur.com/eC2pJ.png" alt="dataframe with time_diff"></p>

<p>Finally, I want to create another column <code>hours_diff</code> that reports the value in <code>time_diff</code> in terms of hours, given as a float. Ignoring microsecond precision, I tried</p>

<pre><code>df['hours_diff'] = df.time_diff.map(lambda t: t.days*24.0 + t.seconds/3600.0)
</code></pre>

<p>as well as </p>

<pre><code>df.loc[df.time_diff.notnull()==True,'hours_diff'] = df.loc[df.time_diff.notnull()==True].time_diff.map(lambda t: t.days*24.0 + t.seconds/3600.0)
</code></pre>

<p>both of which give me</p>

<blockquote>
  <p>AttributeError: 'numpy.timedelta64' object has no attribute 'days'.</p>
</blockquote>

<p>However, if I run the command</p>

<pre><code>print set([type(i) for i in df.time_diff]),
</code></pre>

<p>it tells me that the data types for the values in column <code>time_diff</code> are either <code>pandas.tslib.Timedelta</code> and <code>pandas.tslib.NaTType</code>, neither of which seem to be the <code>numpy.timedelta64</code> type.</p>