Skip to content

Commit c76120f

Browse files
mylesTomAugspurger
authored andcommitted
DOC: update the pandas.DataFrame.pct_change docstring (#20310)
* 💡 Fix some basic stuff from the validator script. * 💡 Add a quick example of the pct_change function. * 💡 Add some see also topics to pct_change. * 💡 Stop showing the private method. * 💡 Add an exmaple of Series and a Series with a fill_method. * 🚨 Fix a mistake with the docstring. * 💡 Update based off feedback from @jorisvandenbossche. * 💡 Update based off feedback from @TomAugspurger. * Previous to prior Note that it's immedate, but configurable. no type on kwargs. Example with periods. Moved See Also.
1 parent bfe6ebc commit c76120f

File tree

1 file changed

+100
-11
lines changed

1 file changed

+100
-11
lines changed

pandas/core/generic.py

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7523,29 +7523,118 @@ def _check_percentile(self, q):
75237523
return q
75247524

75257525
_shared_docs['pct_change'] = """
7526-
Percent change over given number of periods.
7526+
Percentage change between the current and a prior element.
7527+
7528+
Computes the percentage change from the immediately previous row by
7529+
default. This is useful in comparing the percentage of change in a time
7530+
series of elements.
75277531
75287532
Parameters
75297533
----------
75307534
periods : int, default 1
7531-
Periods to shift for forming percent change
7535+
Periods to shift for forming percent change.
75327536
fill_method : str, default 'pad'
7533-
How to handle NAs before computing percent changes
7537+
How to handle NAs before computing percent changes.
75347538
limit : int, default None
7535-
The number of consecutive NAs to fill before stopping
7539+
The number of consecutive NAs to fill before stopping.
75367540
freq : DateOffset, timedelta, or offset alias string, optional
7537-
Increment to use from time series API (e.g. 'M' or BDay())
7541+
Increment to use from time series API (e.g. 'M' or BDay()).
7542+
**kwargs
7543+
Additional keyword arguments are passed into
7544+
`DataFrame.shift` or `Series.shift`.
75387545
75397546
Returns
75407547
-------
7541-
chg : %(klass)s
7548+
chg : Series or DataFrame
7549+
The same type as the calling object.
75427550
7543-
Notes
7544-
-----
7551+
See Also
7552+
--------
7553+
Series.diff : Compute the difference of two elements in a Series.
7554+
DataFrame.diff : Compute the difference of two elements in a DataFrame.
7555+
Series.shift : Shift the index by some number of periods.
7556+
DataFrame.shift : Shift the index by some number of periods.
7557+
7558+
Examples
7559+
--------
7560+
**Series**
7561+
7562+
>>> s = pd.Series([90, 91, 85])
7563+
>>> s
7564+
0 90
7565+
1 91
7566+
2 85
7567+
dtype: int64
7568+
7569+
>>> s.pct_change()
7570+
0 NaN
7571+
1 0.011111
7572+
2 -0.065934
7573+
dtype: float64
7574+
7575+
>>> s.pct_change(periods=2)
7576+
0 NaN
7577+
1 NaN
7578+
2 -0.055556
7579+
dtype: float64
75457580
7546-
By default, the percentage change is calculated along the stat
7547-
axis: 0, or ``Index``, for ``DataFrame`` and 1, or ``minor`` for
7548-
``Panel``. You can change this with the ``axis`` keyword argument.
7581+
See the percentage change in a Series where filling NAs with last
7582+
valid observation forward to next valid.
7583+
7584+
>>> s = pd.Series([90, 91, None, 85])
7585+
>>> s
7586+
0 90.0
7587+
1 91.0
7588+
2 NaN
7589+
3 85.0
7590+
dtype: float64
7591+
7592+
>>> s.pct_change(fill_method='ffill')
7593+
0 NaN
7594+
1 0.011111
7595+
2 0.000000
7596+
3 -0.065934
7597+
dtype: float64
7598+
7599+
**DataFrame**
7600+
7601+
Percentage change in French franc, Deutsche Mark, and Italian lira from
7602+
1980-01-01 to 1980-03-01.
7603+
7604+
>>> df = pd.DataFrame({
7605+
... 'FR': [4.0405, 4.0963, 4.3149],
7606+
... 'GR': [1.7246, 1.7482, 1.8519],
7607+
... 'IT': [804.74, 810.01, 860.13]},
7608+
... index=['1980-01-01', '1980-02-01', '1980-03-01'])
7609+
>>> df
7610+
FR GR IT
7611+
1980-01-01 4.0405 1.7246 804.74
7612+
1980-02-01 4.0963 1.7482 810.01
7613+
1980-03-01 4.3149 1.8519 860.13
7614+
7615+
>>> df.pct_change()
7616+
FR GR IT
7617+
1980-01-01 NaN NaN NaN
7618+
1980-02-01 0.013810 0.013684 0.006549
7619+
1980-03-01 0.053365 0.059318 0.061876
7620+
7621+
Percentage of change in GOOG and APPL stock volume. Shows computing
7622+
the percentage change between columns.
7623+
7624+
>>> df = pd.DataFrame({
7625+
... '2016': [1769950, 30586265],
7626+
... '2015': [1500923, 40912316],
7627+
... '2014': [1371819, 41403351]},
7628+
... index=['GOOG', 'APPL'])
7629+
>>> df
7630+
2016 2015 2014
7631+
GOOG 1769950 1500923 1371819
7632+
APPL 30586265 40912316 41403351
7633+
7634+
>>> df.pct_change(axis='columns')
7635+
2016 2015 2014
7636+
GOOG NaN -0.151997 -0.086016
7637+
APPL NaN 0.337604 0.012002
75497638
"""
75507639

75517640
@Appender(_shared_docs['pct_change'] % _shared_doc_kwargs)

0 commit comments

Comments
 (0)