Understanding pandas.Series.pct_change() method (with examples)

Updated: February 20, 2024 By: Guest Contributor Post a comment

Introduction

In data analysis, understanding the changes in data over time or across categories is crucial. The pandas library in Python, a cornerstone for data manipulation and analysis, provides various methods to analyze and manipulate data. One such method is pct_change(), used in Series objects, to compute the percentage change between consecutive elements. This article explores the pct_change() method in detail, guiding you through basic to advanced examples, equipping you with the knowledge to apply this function effectively to your data analysis tasks.

Syntax & Parameters

The pct_change() method calculates the percentage change between consecutive elements in a pandas Series. This function is particularly useful in financial data analysis, where understanding the rate of return or growth rate over time is essential. However, its applications are not limited to finance; it can be useful in any domain where analyzing the relative change of data points is relevant.

Syntax:

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

Parameters:

  • periods: Number of periods to calculate over (default is 1 for the immediate predecessor). Negative periods will calculate the percentage change in reverse order.
  • fill_method: Method to use for filling NA/NaN values (‘pad’, ‘ffill’ – forward fill, or ‘bfill’ – backward fill).
  • limit: Limit of consecutive NaN values to fill.
  • freq: Frequency strings can be used to conform time series to a specified frequency.

Basic Example

First, ensure you have pandas installed and imported:

import pandas as pd

Create a simple Series and apply the pct_change() method:

series = pd.Series([100, 105, 103, 108, 110])
percent_changes = series.pct_change()
print(percent_changes)

Output:

0 NaN
1 0.050
2 -0.019
3 0.049
4 0.019
dtype: float64

This output shows the percentage change between each consecutive element. The first element is NaN since there is no prior element to compare.

Handling Missing Data

It’s not uncommon to encounter missing data. Let’s see how pct_change() handles NaN values and how we can control this behavior.

series_with_nan = pd.Series([100, NaN, 103, 108, NaN, 110])
percent_changes_with_nan = series_with_nan.pct_change()
print(percent_changes_with_nan)

Output:

0 NaN
1 NaN
2 NaN
3 0.049
4 NaN
5 0.019
dtype: float64

By default, pct_change() will forward fill the missing values. However, this behavior can be altered using the fill_method parameter. You can use 'bfill' to backward fill or None to leave NaN values.

Advanced Examples

Let’s delve deeper with some more complex scenarios where pct_change() can be applied.

Applying pct_change() Over Different Periods

Analyzing the percentage change over periods other than the immediate predecessor can reveal different insights. Here’s an example:

longer_period_series = pd.Series([100, 110, 120, 130, 140])
percent_changes_over_2_periods = longer_period_series.pct_change(periods=2)
print(percent_changes_over_2_periods)

Output:

0 NaN
1 NaN
2 0.200
3 0.182
4 0.167
dtype: float64

This calculates the percentage change every two elements, providing insights into longer-term trends.

Frequency and Date Range

When working with time series data, setting the frequency can help align the data with a specific time interval. Let’s explore this with a date range:

dates = pd.date_range(start='2023-01-01', periods=5, freq='D')
time_series = pd.Series([100, 105, 103, 108, 110], index=dates)
percent_changes_with_freq = time_series.pct_change(freq='D')
print(percent_changes_with_freq)

Output:

2023-01-01 NaN
2023-01-02 0.050
2023-01-03 -0.019
2023-01-04 0.049
2023-01-05 0.019
dtype: float64

This has calculated the daily percentage change over the specified time frame.

Conclusion

The pct_change() method is a versatile tool in pandas that allows for insightful analysis of percentage changes in your data. Whether for financial analysis, studying trends, or any other form of data analysis that requires understanding relative changes, pct_change() can provide valuable perspectives. The key is to experiment with its parameters and apply it to various datasets to uncover hidden trends and insights.