5

Given:

ts = pd.to_datetime(['2014-01-08 08:00:42',
                     '2014-01-08 08:01:00',
                     '2014-01-08 08:01:12'])

Is there a prescribed way to get a result that is the timedelta (preferably total_seconds attribute) between each element?

What I have now seems overly verbose:

pd.Index((pd.Series(ts) - pd.Series(ts).shift())).total_seconds()
# Float64Index([nan, 18.0, 12.0], dtype='float64')

I don't really care about the resulting data structure type, be it a list, Index, or Series.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235

1 Answers1

1

You can use np.diff in this way.

np.diff(ts.values).astype(int) / 1E9

array([ 18.,  12.])

Or

np.append(np.nan, np.diff(ts.values).astype(int) / 1E9)

array([ nan,  18.,  12.])
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • I prepended an `np.nan` but adds to the verbosity – piRSquared Sep 19 '17 at 17:40
  • You need `np.append(np.nan, (np.diff(ts.values) // 1E9).astype(int))` I think? – Zero Sep 19 '17 at 17:43
  • Since, we are at it, `[np.nan] + (np.diff(ts.values) // 1E9).tolist()` perhaps? – Zero Sep 19 '17 at 17:46
  • I don't think there is a difference between your first suggestion and what I proposed. I used `astype(int)` to cast away from `timedelta[ns]`. After that, I don't think the ordering of when you do the casting changes the final outcome. For your second suggestion, I can't be certain without testing... but I'm willing to bet there is a performance hit. – piRSquared Sep 19 '17 at 17:49
  • 1
    Ah, `np.diff(ts.values).astype(int) // 1E9` gives me `array([ 0., -1.])` hence asking to move `astype` – Zero Sep 19 '17 at 17:51
  • Whoops `//` is unnecessary. I removed it. I would be different for python 2 vs 3. I'm assuming you're using python 2. – piRSquared Sep 19 '17 at 17:53
  • Yes, single `/` too doesn't work on Python2 atleast. – Zero Sep 19 '17 at 17:56