- API reference
- Series
- pandas.Serie...
pandas.Series.pct_change#
- Series.pct_change(periods=1,fill_method=<no_default>,limit=<no_default>,freq=None,**kwargs)[source]#
Fractional change between the current and a prior element.
Computes the fractional change from the immediately previous row bydefault. This is useful in comparing the fraction of change in a timeseries of elements.
Note
Despite the name of this method, it calculates fractional change(also known as per unit change or relative change) and notpercentage change. If you need the percentage change, multiplythese values by 100.
- Parameters:
- periodsint, default 1
Periods to shift for forming percent change.
- fill_method{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default ‘pad’
How to handle NAsbefore computing percent changes.
Deprecated since version 2.1:All options offill_method are deprecated exceptfill_method=None.
- limitint, default None
The number of consecutive NAs to fill before stopping.
Deprecated since version 2.1.
- freqDateOffset, timedelta, or str, optional
Increment to use from time series API (e.g. ‘ME’ or BDay()).
- **kwargs
Additional keyword arguments are passed intoDataFrame.shift orSeries.shift.
- Returns:
- Series or DataFrame
The same type as the calling object.
See also
Series.diffCompute the difference of two elements in a Series.
DataFrame.diffCompute the difference of two elements in a DataFrame.
Series.shiftShift the index by some number of periods.
DataFrame.shiftShift the index by some number of periods.
Examples
Series
>>>s=pd.Series([90,91,85])>>>s0 901 912 85dtype: int64
>>>s.pct_change()0 NaN1 0.0111112 -0.065934dtype: float64
>>>s.pct_change(periods=2)0 NaN1 NaN2 -0.055556dtype: float64
See the percentage change in a Series where filling NAs with lastvalid observation forward to next valid.
>>>s=pd.Series([90,91,None,85])>>>s0 90.01 91.02 NaN3 85.0dtype: float64
>>>s.ffill().pct_change()0 NaN1 0.0111112 0.0000003 -0.065934dtype: float64
DataFrame
Percentage change in French franc, Deutsche Mark, and Italian lira from1980-01-01 to 1980-03-01.
>>>df=pd.DataFrame({...'FR':[4.0405,4.0963,4.3149],...'GR':[1.7246,1.7482,1.8519],...'IT':[804.74,810.01,860.13]},...index=['1980-01-01','1980-02-01','1980-03-01'])>>>df FR GR IT1980-01-01 4.0405 1.7246 804.741980-02-01 4.0963 1.7482 810.011980-03-01 4.3149 1.8519 860.13
>>>df.pct_change() FR GR IT1980-01-01 NaN NaN NaN1980-02-01 0.013810 0.013684 0.0065491980-03-01 0.053365 0.059318 0.061876
Percentage of change in GOOG and APPL stock volume. Shows computingthe percentage change between columns.
>>>df=pd.DataFrame({...'2016':[1769950,30586265],...'2015':[1500923,40912316],...'2014':[1371819,41403351]},...index=['GOOG','APPL'])>>>df 2016 2015 2014GOOG 1769950 1500923 1371819APPL 30586265 40912316 41403351
>>>df.pct_change(axis='columns',periods=-1) 2016 2015 2014GOOG 0.179241 0.094112 NaNAPPL -0.252395 -0.011860 NaN