title: Calculate time difference between rows in python
body: <p>Trying to calculate the time difference between rows if several conditions apply:</p>
  
  <pre><code>df['open_time'] = pd.to_datetime(df['open_time'], errors='coerce')
  df['Time_diff'] = pd.to_datetime(df['Time_diff'], errors='coerce')
  
  for i in range(1, len(df)):
  if df.loc[i, 'JOB_ID'] == df.loc[i-1, 'JOB_ID'] and df.loc[i, 'STATION_IDX'] &gt; df.loc[i-1, 'STATION_IDX']:
      df['Time_diff'] = df.loc[i, 'open_time'] - df.loc[i-1, 'open_time']
  </code></pre>
  
  <p>open_time is a simple time in the day HH:mm:ss at which an action was performed, that's all...</p>
  
  <p>The original dataset is:</p>
  
  <pre><code>JOB_ID  DDMMYY  STATION_IDX open_time
  121663240   04-02-19    25  5:02:19
  121663240   04-02-19    26  5:04:00
  121663240   04-02-19    27  5:04:42
  121651974   04-02-19    25  6:08:15
  121651974   04-02-19    27  6:10:28
  </code></pre>
  
  <p>I cannot understand why I keep getting 'NaT' for all the rows of Time_diff</p>
  
  <pre><code>       JOB_ID Time_diff
  0   121663240       NaT
  1   121663240       NaT
  2   121663240       NaT
  3   121651974       NaT
  4   121651974       NaT
  5   121682840       NaT
  6   121682840       NaT
  </code></pre>
  
  <p>I can't seem to find any answer in Google that will fit my calculation row.</p>
  
  <p>The expected result I hope to get for the above dataset is:</p>
  
  <pre><code>JOB_ID ddmmyy   25 to 26    26 to 27    25 to 27
  121663240   04-02-2019  101 42  143
  121651974   04-02-2019  NaN NaN 133
  </code></pre>