python - Datetime calculation between two columns in python pandas
<p>I have a DataFrame like this: 
And this DataFrame is called<code>df_NoMissing_IDV</code>.</p>

<pre><code>NoDemande   NoUsager  Sens  IdVehiculeUtilise  Fait  HeureArriveeSurSite   HeureEffective      Periods
42196000013  000001    +         287VÃ©h          1  11/07/2015 08:02:07  11/07/2015 08:02:13    Matin
42196000013  000001    -         287VÃ©h          1  11/07/2015 08:17:09  11/07/2015 08:17:13    Matin
42196000002  000314    +         263VÃ©h          1  11/07/2015 09:37:43  11/07/2015 09:53:37    Matin
42196000016  002372    +         287VÃ©h          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000015  000466    +         287VÃ©h          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000002  000314    -         263VÃ©h          1  11/07/2015 10:25:17  11/07/2015 10:38:11    Matin
42196000015  000466    -         287VÃ©h          1  11/07/2015 10:48:51  11/07/2015 10:51:30    Matin
42196000016  002372    -         287VÃ©h          1  11/07/2015 11:40:56  11/07/2015 11:41:01    Matin
42196000004  002641    +         263VÃ©h          1  11/07/2015 13:39:29  11/07/2015 13:52:50    Soir
42196000004  002641    -         263VÃ©h          1  11/07/2015 13:59:56  11/07/2015 14:07:41    Soir  
</code></pre>

<p>I need to get the marge between column <code>HeureArriveeSurSite</code> and <code>HeureEffective</code>,and they are already <code>datetime.datetime()</code> data.</p>

<p>And here is a new <code>DataFrame</code> called <code>df1</code>.</p>

<pre><code>df1 = df_NoMissing_IDV[(df_NoMissing_IDV['Sens'] == '+') &amp; (df_NoMissing_IDV['Periods'] == 'Matin')]
</code></pre>

<p>And <code>df1</code> looks like this:</p>

<pre><code>NoDemande   NoUsager  Sens  IdVehiculeUtilise  Fait  HeureArriveeSurSite   HeureEffective      Periods
42196000013  000001    +         287VÃ©h          1  11/07/2015 08:02:07  11/07/2015 08:02:13    Matin
42196000002  000314    +         263VÃ©h          1  11/07/2015 09:37:43  11/07/2015 09:53:37    Matin
42196000016  002372    +         287VÃ©h          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000015  000466    +         287VÃ©h          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
</code></pre>

<p>Since they all are <code>datetime.datetime()</code> data, I tried to do the subtraction directly with:</p>

<pre><code>df_NoMissing_IDV['DureeService'] = df1['HeureEffective']-df1['HeureArriveeSurSite']
</code></pre>

<p>But it returned <code>TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'</code></p>

<p>And I also tried to do the calculation with <code>datetime.time()</code> type, and it returned with <code>TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'</code> What should I do with it?</p>

<p><strong>EDIT</strong></p>

<p>I convert columns in <code>df1</code> to <code>datetime()</code>:</p>

<pre><code>df1.HeureArriveeSurSite = pd.to_datetime(df1.HeureArriveeSurSite)
df1.HeureEffective = pd.to_datetime(df1.HeureEffective)
</code></pre>

<p>But the next step is still wrong which returned: <code>ValueError: cannot reindex from a duplicate axis</code></p>

<p>And if I convert columns in <code>df_NoMissing_IDV</code> to <code>datetime()</code>:</p>

<pre><code>df_NoMissing_IDV.HeureArriveeSurSite = pd.to_datetime(df_NoMissing_IDV.HeureArriveeSurSite)
df_NoMissing_IDV.HeureEffective = pd.to_datetime(df_NoMissing_IDV.HeureEffective)
</code></pre>

<p>The same problem remains.</p>

<p>Any help will be appreciated~</p>

<p>I think that the cause of the error is that you have some dplicates in your data. </p>

<p>Try two things out:</p>

<pre><code>df_NoMissing_IDV['DureeService'] = df1['HeureEffective'].values -df1['HeureArriveeSurSite'].values
</code></pre>

<p>Or:</p>

<pre><code>df1 = df1.reset_index()
</code></pre>

<p><strong>EDIT:</strong>
What you can also try is <code>timedelta</code>:</p>

<pre><code>&gt;&gt;&gt; import datetime
&gt;&gt;&gt; time_difference = df1['HeureEffective']-df1['HeureArriveeSurSite']
&gt;&gt;&gt; time_difference_in_seconds = time_difference / timedelta(seconds=1)
</code></pre>

python datetime pandas dataframe