pandas 1.4.2

NotesParametersReturnsBackRef
diff(self, periods: 'int' = 1) -> 'Series'

Calculates the difference of a Series element compared with another element in the Series (default is element in previous row).

Notes

For boolean dtypes, this uses operator.xor rather than operator.sub . The result is calculated according to current dtype in Series, however dtype of the result is always float64.

Parameters

periods : int, default 1

Periods to shift for calculating difference, accepts negative values.

Returns

Series

First differences of the Series.

First discrete difference of element.

See Also

DataFrame.diff

First discrete difference of object.

Series.pct_change

Percent change over given number of periods.

Series.shift

Shift index by desired number of periods with an optional time freq.

Examples

Difference with previous row

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 1, 2, 3, 5, 8])
... s.diff() 0 NaN 1 0.0 2 1.0 3 1.0 4 2.0 5 3.0 dtype: float64

Difference with 3rd previous row

This example is valid syntax, but we were not able to check execution
>>> s.diff(periods=3)
0    NaN
1    NaN
2    NaN
3    2.0
4    4.0
5    6.0
dtype: float64

Difference with following row

This example is valid syntax, but we were not able to check execution
>>> s.diff(periods=-1)
0    0.0
1   -1.0
2   -1.0
3   -2.0
4   -3.0
5    NaN
dtype: float64

Overflow in input dtype

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 0], dtype=np.uint8)
... s.diff() 0 NaN 1 255.0 dtype: float64
See :

Back References

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

pandas.core.frame.DataFrame.diff pandas.core.generic.NDFrame.pct_change

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: /pandas/core/series.py#2612
type: <class 'function'>
Commit: