dask 2021.10.0

NotesParametersReturnsBackRef
diff(a, n=1, axis=-1, prepend=None, append=None)

This docstring was copied from numpy.diff.

Some inconsistencies with the Dask version may exist.

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.

Notes

Type is preserved for boolean arrays, so the result will contain :None:None:`False` when consecutive elements are the same and :None:None:`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)  # doctest: +SKIP
>>> np.diff(u8_arr)  # doctest: +SKIP
array([255], dtype=uint8)
>>> u8_arr[1,...] - u8_arr[0,...]  # doctest: +SKIP
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)  # doctest: +SKIP
>>> np.diff(i16_arr)  # doctest: +SKIP
array([-1], dtype=int16)

Parameters

a : array_like

Input array

n : int, optional

The number of times values are differenced. If zero, the input is returned as-is.

axis : int, optional

The axis along which the difference is taken, default is the last axis.

prepend, append : array_like, optional

Values to prepend or append to a along 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 match a except along axis.

versionadded

Returns

diff : ndarray

The n-th differences. The shape of the output is the same as a except along :None:None:`axis` where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is :None:None:`datetime64`, which results in a :None:None:`timedelta64` output array.

Calculate the n-th discrete difference along the given axis.

See Also

cumsum
ediff1d
gradient

Examples

This example is valid syntax, but we were not able to check execution
>>> x = np.array([1, 2, 4, 7, 0])  # doctest: +SKIP
... np.diff(x) # doctest: +SKIP array([ 1, 2, 3, -7])
This example is valid syntax, but we were not able to check execution
>>> np.diff(x, n=2)  # doctest: +SKIP
array([  1,   1, -10])
This example is valid syntax, but we were not able to check execution
>>> x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]])  # doctest: +SKIP
... np.diff(x) # doctest: +SKIP array([[2, 3, 4], [5, 1, 2]])
This example is valid syntax, but we were not able to check execution
>>> np.diff(x, axis=0)  # doctest: +SKIP
array([[-1,  2,  0, -2]])
This example is valid syntax, but we were not able to check execution
>>> x = np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64)  # doctest: +SKIP
... np.diff(x) # doctest: +SKIP array([1, 1], dtype='timedelta64[D]')
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.routines.ediff1d dask.array.reductions.cumsum dask.array.routines.diff

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /dask/array/routines.py#526
type: <class 'function'>
Commit: