Python | Pandas Series.pct_change()

Pandas pct_change() method is applied on series with numeric data to calculate Percentage change after n number of elements. By default, it calculates percentage change of current element from the previous element. (Current-Previous/Previous) * 100.

First, n(n=period) values are always NaN, since there is no previous value to calculate change.

Syntax: Series.pct_change(periods=1, fill_method=’pad’, limit=None)

Parameters:
periods: Defines gap between current and previous value. Default is 1
fill_method: Defines method used to handle null values
limit: Number of consecutive NaN values to fill before stopping.

Return type: Numeric series with percentage change

Example #1:

In this method, a Series is created from Python list using Pandas Series(). The series doesn’t contain any null value and hence pct_change() method is called directly with default value of period parameter, that is 1.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# creating list
list = [10, 14, 20, 25, 12.5, 13, 0, 50]
  
# creating series
series = pd.Series(list)
  
# calling method
result = series.pct_change()
  
# display
result

chevron_right


Output:

0         NaN
1    0.400000
2    0.428571
3    0.250000
4   -0.500000
5    0.040000
6   -1.000000
7         inf
dtype: float64

As shown in output, first n values are always equal to NaN. Rest of the values are equal to the percentage change in Old values and are stored at the same position as caller series.
Note: Since second last value was 0, the Percentage change is inf. inf stands for infinite.
Using the formula, pct_change= x-0/0 = Infinte

 
Example #2: Handling Null values

In this example, some null values are also created using Numpy’s np.nan method and passed to the list. ‘bfill‘ is passed to fill_method. bfill stands for Back fill and will fill Null values with values at their very next position.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# creating list
list =[10, np.nan, 14, 20, 25, 12.5, 13, 0, 50]
  
# creating series
series = pd.Series(list)
  
# calling method
result = series.pct_change(fill_method ='bfill')
  
# display
result

chevron_right


Output:

0         NaN
1    0.400000
2    0.000000
3    0.428571
4    0.250000
5   -0.500000
6    0.040000
7   -1.000000
8         inf
dtype: float64

As it can be seen in output, value at position 1 is 40 because NaN was replaced by 14. Hence, (14-10/10) *100 = 40. The very next value is 0 because percentage change in 14 and 14 is 0.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.