pandas 1.4.2

NotesParametersReturnsBackRef
diff(self, periods: 'int' = 1, axis: 'Axis' = 0) -> 'DataFrame'

Calculates the difference of a Dataframe element compared with another element in the Dataframe (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 Dataframe, however dtype of the result is always float64.

Parameters

periods : int, default 1

Periods to shift for calculating difference, accepts negative values.

axis : {0 or 'index', 1 or 'columns'}, default 0

Take difference over rows (0) or columns (1).

Returns

Dataframe

First differences of the Series.

First discrete difference of element.

See Also

Dataframe.pct_change

Percent change over given number of periods.

Dataframe.shift

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

Series.diff

First discrete difference of object.

Examples

Difference with previous row

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],
...  'b': [1, 1, 2, 3, 5, 8],
...  'c': [1, 4, 9, 16, 25, 36]})
... df a b c 0 1 1 1 1 2 1 4 2 3 2 9 3 4 3 16 4 5 5 25 5 6 8 36
This example is valid syntax, but we were not able to check execution
>>> df.diff()
     a    b     c
0  NaN  NaN   NaN
1  1.0  0.0   3.0
2  1.0  1.0   5.0
3  1.0  1.0   7.0
4  1.0  2.0   9.0
5  1.0  3.0  11.0

Difference with previous column

This example is valid syntax, but we were not able to check execution
>>> df.diff(axis=1)
    a  b   c
0 NaN  0   0
1 NaN -1   3
2 NaN -1   7
3 NaN -1  13
4 NaN  0  20
5 NaN  2  28

Difference with 3rd previous row

This example is valid syntax, but we were not able to check execution
>>> df.diff(periods=3)
     a    b     c
0  NaN  NaN   NaN
1  NaN  NaN   NaN
2  NaN  NaN   NaN
3  3.0  2.0  15.0
4  3.0  4.0  21.0
5  3.0  6.0  27.0

Difference with following row

This example is valid syntax, but we were not able to check execution
>>> df.diff(periods=-1)
     a    b     c
0 -1.0  0.0  -3.0
1 -1.0 -1.0  -5.0
2 -1.0 -1.0  -7.0
3 -1.0 -2.0  -9.0
4 -1.0 -3.0 -11.0
5  NaN  NaN   NaN

Overflow in input dtype

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'a': [1, 0]}, dtype=np.uint8)
... df.diff() a 0 NaN 1 255.0
See :

Back References

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

pandas.core.generic.NDFrame.pct_change pandas.core.series.Series.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: /pandas/core/frame.py#8447
type: <class 'function'>
Commit: