Skip to content

Commit 302fda4

Browse files
adatasetadayTomAugspurger
authored andcommitted
DOC: update the pandas.DataFrame.diff docstring (pandas-dev#20227)
* DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * DOC: update the pandas.DataFrame.diff docstring * Cleanup
1 parent c791a84 commit 302fda4

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

Diff for: pandas/core/frame.py

+73-3
Original file line numberDiff line numberDiff line change
@@ -4770,20 +4770,90 @@ def melt(self, id_vars=None, value_vars=None, var_name=None,
47704770

47714771
def diff(self, periods=1, axis=0):
47724772
"""
4773-
1st discrete difference of object
4773+
First discrete difference of element.
4774+
4775+
Calculates the difference of a DataFrame element compared with another
4776+
element in the DataFrame (default is the element in the same column
4777+
of the previous row).
47744778
47754779
Parameters
47764780
----------
47774781
periods : int, default 1
4778-
Periods to shift for forming difference
4782+
Periods to shift for calculating difference, accepts negative
4783+
values.
47794784
axis : {0 or 'index', 1 or 'columns'}, default 0
47804785
Take difference over rows (0) or columns (1).
47814786
4782-
.. versionadded:: 0.16.1
4787+
.. versionadded:: 0.16.1.
47834788
47844789
Returns
47854790
-------
47864791
diffed : DataFrame
4792+
4793+
See Also
4794+
--------
4795+
Series.diff: First discrete difference for a Series.
4796+
DataFrame.pct_change: Percent change over given number of periods.
4797+
DataFrame.shift: Shift index by desired number of periods with an
4798+
optional time freq.
4799+
4800+
Examples
4801+
--------
4802+
Difference with previous row
4803+
4804+
>>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],
4805+
... 'b': [1, 1, 2, 3, 5, 8],
4806+
... 'c': [1, 4, 9, 16, 25, 36]})
4807+
>>> df
4808+
a b c
4809+
0 1 1 1
4810+
1 2 1 4
4811+
2 3 2 9
4812+
3 4 3 16
4813+
4 5 5 25
4814+
5 6 8 36
4815+
4816+
>>> df.diff()
4817+
a b c
4818+
0 NaN NaN NaN
4819+
1 1.0 0.0 3.0
4820+
2 1.0 1.0 5.0
4821+
3 1.0 1.0 7.0
4822+
4 1.0 2.0 9.0
4823+
5 1.0 3.0 11.0
4824+
4825+
Difference with previous column
4826+
4827+
>>> df.diff(axis=1)
4828+
a b c
4829+
0 NaN 0.0 0.0
4830+
1 NaN -1.0 3.0
4831+
2 NaN -1.0 7.0
4832+
3 NaN -1.0 13.0
4833+
4 NaN 0.0 20.0
4834+
5 NaN 2.0 28.0
4835+
4836+
Difference with 3rd previous row
4837+
4838+
>>> df.diff(periods=3)
4839+
a b c
4840+
0 NaN NaN NaN
4841+
1 NaN NaN NaN
4842+
2 NaN NaN NaN
4843+
3 3.0 2.0 15.0
4844+
4 3.0 4.0 21.0
4845+
5 3.0 6.0 27.0
4846+
4847+
Difference with following row
4848+
4849+
>>> df.diff(periods=-1)
4850+
a b c
4851+
0 -1.0 0.0 -3.0
4852+
1 -1.0 -1.0 -5.0
4853+
2 -1.0 -1.0 -7.0
4854+
3 -1.0 -2.0 -9.0
4855+
4 -1.0 -3.0 -11.0
4856+
5 NaN NaN NaN
47874857
"""
47884858
bm_axis = self._get_block_manager_axis(axis)
47894859
new_data = self._data.diff(n=periods, axis=bm_axis)

0 commit comments

Comments
 (0)