Pandas Tutorial Pandas References

Pandas Series - pct_change() function



The Pandas Series pct_change() function computes the percentage change between the current and a prior element by default. This is useful in comparing the percentage of change in a time series of elements.

Syntax

Series.pct_change(periods=1, fill_method='pad', 
                  limit=None, freq=None)

Parameters

periods Optional. Specify the period to shift for calculating percent change. Default: 1
fill_method Optional. Specify how to handle NAs before computing percent changes. Default: 'pad'. It can take values from {'backfill', 'bfill', 'pad', 'ffill', None}. pad / ffill: use last valid observation to fill gap. backfill / bfill: use next valid observation to fill gap.
limit Optional. Specify the number of consecutive NAs to fill before stopping. Default is None.
freq Optional. A DateOffset, timedelta, or str to specify increment to use from time series API (e.g. 'M' or BDay()). Default is None.

Return Value

Returns the same type as the calling object with percentage change of element.

Example: Percentage change of elements of a Series

In the example below, the pct_change() function is used to calculate the percentage change of elements of a series with specified period.

import pandas as pd
import numpy as np

GDP = pd.Series([1.5, 2.5, 3.5, 1.5, 2.5, -1])

print("The Series contains:")
print(GDP)

#percentage change of element with period = 1
print("\nGDP.pct_change() returns:")
print(GDP.pct_change())

#percentage change of element with period = 2
print("\nGDP.pct_change(periods=2) returns:")
print(GDP.pct_change(periods=2))

The output of the above code will be:

The Series contains:
0    1.5
1    2.5
2    3.5
3    1.5
4    2.5
5   -1.0
dtype: float64

GDP.pct_change() returns:
0         NaN
1    0.666667
2    0.400000
3   -0.571429
4    0.666667
5   -1.400000
dtype: float64

GDP.pct_change(periods=2) returns:
0         NaN
1         NaN
2    1.333333
3   -0.400000
4   -0.285714
5   -1.666667
dtype: float64

Example: using pct_change() on selected series in a DataFrame

Similarly, the pct_change() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "GDP": [1.5, 2.5, 3.5, 1.5, 2.5, -1],
  "GNP": [1, 2, 3, 3, 2, -1],
  "HPI": [2, 3, 2, np.NaN, 2, 2]},
  index= ["2015", "2016", "2017", 
          "2018", "2019", "2020"]
)

print("The DataFrame is:")
print(df)

#percentage change of element of 'GDP' column
print("\ndf['GDP'].pct_change() returns:")
print(df['GDP'].pct_change())

The output of the above code will be:

The DataFrame is:
      GDP  GNP  HPI
2015  1.5    1  2.0
2016  2.5    2  3.0
2017  3.5    3  2.0
2018  1.5    3  NaN
2019  2.5    2  2.0
2020 -1.0   -1  2.0

df['GDP'].pct_change() returns:
2015         NaN
2016    0.666667
2017    0.400000
2018   -0.571429
2019    0.666667
2020   -1.400000
Name: GDP, dtype: float64

❮ Pandas Series - Functions