Skip to content

Commit 8c77238

Browse files
adatasetadayjreback
authored andcommitted
Docstring pandas.series.diff (pandas-dev#20238)
1 parent 4271757 commit 8c77238

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

pandas/core/series.py

+49-2
Original file line numberDiff line numberDiff line change
@@ -1607,16 +1607,63 @@ 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+
Series.shift: Shift index by desired number of periods with an
1629+
optional time freq.
1630+
DataFrame.diff: First discrete difference of object
1631+
1632+
Examples
1633+
--------
1634+
Difference with previous row
1635+
1636+
>>> s = pd.Series([1, 1, 2, 3, 5, 8])
1637+
>>> s.diff()
1638+
0 NaN
1639+
1 0.0
1640+
2 1.0
1641+
3 1.0
1642+
4 2.0
1643+
5 3.0
1644+
dtype: float64
1645+
1646+
Difference with 3rd previous row
1647+
1648+
>>> s.diff(periods=3)
1649+
0 NaN
1650+
1 NaN
1651+
2 NaN
1652+
3 2.0
1653+
4 4.0
1654+
5 6.0
1655+
dtype: float64
1656+
1657+
Difference with following row
1658+
1659+
>>> s.diff(periods=-1)
1660+
0 0.0
1661+
1 -1.0
1662+
2 -1.0
1663+
3 -2.0
1664+
4 -3.0
1665+
5 NaN
1666+
dtype: float64
16201667
"""
16211668
result = algorithms.diff(com._values_from_object(self), periods)
16221669
return self._constructor(result, index=self.index).__finalize__(self)

0 commit comments

Comments
 (0)