Skip to content

Commit 2f40cff

Browse files
committed
DOC: update the pandas.Series.diff docstring
1 parent 15bcb7d commit 2f40cff

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

Diff for: pandas/core/series.py

+47-2
Original file line numberDiff line numberDiff line change
@@ -1607,16 +1607,61 @@ def cov(self, other, min_periods=None):
16071607

16081608
def diff(self, periods=1):
16091609
"""
1610-
1st discrete difference of object
1610+
First discrete difference of element.
1611+
1612+
Calculates the difference of a Series element compared with another
1613+
element in the Series (default is element in previous row).
16111614
16121615
Parameters
16131616
----------
16141617
periods : int, default 1
1615-
Periods to shift for forming difference
1618+
Periods to shift for calculating difference, accepts negative
1619+
values.
16161620
16171621
Returns
16181622
-------
16191623
diffed : Series
1624+
1625+
See Also
1626+
--------
1627+
Series.pct_change: Percent change over given number of periods.
1628+
DataFrame.diff: First discrete difference of object
1629+
1630+
Examples
1631+
--------
1632+
Difference with previous row
1633+
1634+
>>> s = pd.Series([1, 1, 2, 3, 5, 8])
1635+
>>> s.diff()
1636+
0 NaN
1637+
1 0.0
1638+
2 1.0
1639+
3 1.0
1640+
4 2.0
1641+
5 3.0
1642+
dtype: float64
1643+
1644+
Difference with 3rd previous row
1645+
1646+
>>> s.diff(periods=3)
1647+
0 NaN
1648+
1 NaN
1649+
2 NaN
1650+
3 2.0
1651+
4 4.0
1652+
5 6.0
1653+
dtype: float64
1654+
1655+
Difference with following row
1656+
1657+
>>> s.diff(periods=-1)
1658+
0 0.0
1659+
1 -1.0
1660+
2 -1.0
1661+
3 -2.0
1662+
4 -3.0
1663+
5 NaN
1664+
dtype: float64
16201665
"""
16211666
result = algorithms.diff(com._values_from_object(self), periods)
16221667
return self._constructor(result, index=self.index).__finalize__(self)

0 commit comments

Comments
 (0)