Use `np.diff` but assume the input starts with an extra zero

Given for example:

t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])

We want to compute the consecutive differences in t, including the diff from 0. to the first element in t.

The question gave this way of accomplishing this:

>>> np.diff(np.hstack((0, t)))

And it could be this too:

>>> np.hstack((t[0], np.diff(t)))

But the obscurely-named function ediff1d can do it in one function call:

>>> np.ediff1d(t, to_begin=t[0])
array([ 1.1,  0.9,  2.5,  0.4,  0.3])

Prepending t[0] to the result is the same as computing the difference t[0] - 0., of course. (Assuming t is nonempty).


Timings (not the motivation of the question, but I was curious)

import numpy as np
t = np.random.randn(10000)
%timeit np.diff(np.concatenate(([0], t)))
10000 loops, best of 3: 23.1 µs per loop
%timeit np.diff(np.hstack((0, t)))
10000 loops, best of 3: 31.2 µs per loop
%timeit np.ediff1d(t, to_begin=t[0])
10000 loops, best of 3: 92 µs per loop

As of 2019, np.diff has the arguments prepend and append that can add a certain value to the array before differentiation. See the docs

This would append the first value to the array, hence the diff operation would return something of len(t) that starts with 0.

>>> t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])
>>> np.diff(t, prepend=t[0])
array([0. , 0.9, 2.5, 0.4, 0.3])

The prepend argument can take other values.

Tags:

Python

Numpy