bundles / numpy 2.4.4 / numpy / diff
_ArrayFunctionDispatcher
numpy:diff
Signature
def diff ( a , n = 1 , axis = -1 , prepend = <no value> , append = <no value> ) Summary
Calculate the n-th discrete difference along the given axis.
Extended Summary
The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively.
Parameters
a: array_likeInput array
n: int, optionalThe number of times values are differenced. If zero, the input is returned as-is.
axis: int, optionalThe axis along which the difference is taken, default is the last axis.
prepend, append: array_like, optionalValues to prepend or append to
aalong axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must matchaexcept along axis.
Returns
diff: ndarrayThe n-th differences. The shape of the output is the same as
aexcept alongaxiswhere the dimension is smaller byn. The type of the output is the same as the type of the difference between any two elements ofa. This is the same as the type ofain most cases. A notable exception isdatetime64, which results in atimedelta64output array.
Notes
Type is preserved for boolean arrays, so the result will contain False when consecutive elements are the same and True when they differ.
For unsigned integer arrays, the results will also be unsigned. This should not be surprising, as the result is consistent with calculating the difference directly:
>>> u8_arr = np.array([1, 0], dtype=np.uint8) >>> np.diff(u8_arr) array([255], dtype=uint8) >>> u8_arr[1,...] - u8_arr[0,...] np.uint8(255)
If this is not desirable, then the array should be cast to a larger integer type first:
>>> i16_arr = u8_arr.astype(np.int16) >>> np.diff(i16_arr) array([-1], dtype=int16)
Examples
import numpy as np x = np.array([1, 2, 4, 7, 0]) np.diff(x) np.diff(x, n=2)✓
x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]]) np.diff(x) np.diff(x, axis=0)✓
x = np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64) np.diff(x)✓
See also
- cumsum
- ediff1d
- gradient
Aliases
-
numpy.diff