Skip to main content

11  Shift-based Methods

So far we have been performing operations using values within the same row, or by operating on all values in a given column.

But what if we want to compare a value in one row to corresponding values in the rows below or above?

We can use "shift-based" methods for this purpose.

Let’s consider this simple example time series dataset:

from pandas import DataFrame

gdp_df = DataFrame([
    {"year": 1990, "gdp": 100},
    {"year": 1991, "gdp": 105},
    {"year": 1992, "gdp": 110},
    {"year": 1993, "gdp": 115},
    {"year": 1994, "gdp": 110}

])
gdp_df.head()
year gdp
0 1990 100
1 1991 105
2 1992 110
3 1993 115
4 1994 110

Before performing shift-based methods, because row order matters, it is important to ensure the rows are sorted in the proper order (usually in ascending order by date).

# sorting by year for good measure:
gdp_df.sort_values(by=["year"], ascending=True, inplace=True)
gdp_df.head()
year gdp
0 1990 100
1 1991 105
2 1992 110
3 1993 115
4 1994 110

We can use the dataframe’s shift method to reference a corresponding value in another row above or below by specifying the number of rows above or below via the periods parameter.

We use positive numbers to reference rows above, and negative numbers to reference cells below:

gdp_df["gdp"].shift(periods=1) # 1 or -1 depending on order
0      NaN
1    100.0
2    105.0
3    110.0
4    115.0
Name: gdp, dtype: float64
gdp_df["gdp"].shift(periods=-1) # 1 or -1 depending on order
0    105.0
1    110.0
2    115.0
3    110.0
4      NaN
Name: gdp, dtype: float64

11.1 Growth and Percent Change

By comparing current against previous values, this allows us to perform an ad-hoc calculation of percent growth from one period to the next.

Here we illustrate the methods one step at a time.

First we create a new column of previous values, using the shift method:

# creating a new column using previous values:
gdp_df["gdp_prev"] = gdp_df["gdp"].shift(periods=1)
gdp_df
year gdp gdp_prev
0 1990 100 NaN
1 1991 105 100.0
2 1992 110 105.0
3 1993 115 110.0
4 1994 110 115.0

Next we calculate change from one period to the next, by subtracting the previous values from the current values:

# calculating change:
gdp_df["gdp_change"] = gdp_df["gdp"] - gdp_df["gdp_prev"]
gdp_df
year gdp gdp_prev gdp_change
0 1990 100 NaN NaN
1 1991 105 100.0 5.0
2 1992 110 105.0 5.0
3 1993 115 110.0 5.0
4 1994 110 115.0 -5.0

Finally we calculate percent change by dividing the change over the previous value:

# calculating percent change:
#gdp_df["gdp_growth"] = (gdp_df["gdp"] - gdp_df["gdp_prev"]) / gdp_df["gdp_prev"]

gdp_df["gdp_pct_change"] = gdp_df["gdp_change"] / gdp_df["gdp_prev"]
gdp_df
year gdp gdp_prev gdp_change gdp_pct_change
0 1990 100 NaN NaN NaN
1 1991 105 100.0 5.0 0.050000
2 1992 110 105.0 5.0 0.047619
3 1993 115 110.0 5.0 0.045455
4 1994 110 115.0 -5.0 -0.043478

11.1.1 The pct_change Method

Even though we are able to perform this growth calculation ourselves, we should know the DataFrame has a dedicated pct_change method for this purpose, which allows us to skip the intermediate steps:

# equivalent, leveraging the pct_change method:
gdp_df["gdp_pct_change"] = gdp_df["gdp"].pct_change(periods=1)

gdp_df[["year", "gdp", "gdp_pct_change"]]
year gdp gdp_pct_change
0 1990 100 NaN
1 1991 105 0.050000
2 1992 110 0.047619
3 1993 115 0.045455
4 1994 110 -0.043478

11.2 Cumulative Growth

Alright, we have studied how to calculate growth from one period to another, but what about calculating cumulative growth over the entire time period?

To calculate cumulative growth for a particular period, we can use thecumprod method (or sometimes the product method, depending on the use case). When calculating the cumulative product, each value gets multiplied by the values that follow, in succession.

Before we calculate a product, to make the multiplication work, we’ll first need to express the growth numbers relative to 1, instead of 0. We’ll also need to fill in the initial null value with a 1, so the first period represents 100%.

Let’s break this down one step at a time, to illustrate each method, before putting them all together at the end.

First we overwrite the initial null value that results from there being no previous row for the first row:

gdp_df.loc[0, "gdp_pct_change"] = 0

gdp_df
year gdp gdp_prev gdp_change gdp_pct_change
0 1990 100 NaN NaN 0.000000
1 1991 105 100.0 5.0 0.050000
2 1992 110 105.0 5.0 0.047619
3 1993 115 110.0 5.0 0.045455
4 1994 110 115.0 -5.0 -0.043478

Then we express growth relative to one instead of zero (so we can calculate cumulative product later):

gdp_df["gdp_pct_change"] = gdp_df["gdp_pct_change"] + 1

gdp_df
year gdp gdp_prev gdp_change gdp_pct_change
0 1990 100 NaN NaN 1.000000
1 1991 105 100.0 5.0 1.050000
2 1992 110 105.0 5.0 1.047619
3 1993 115 110.0 5.0 1.045455
4 1994 110 115.0 -5.0 0.956522

Finally, we calculate the cumulative product of the growth:

#gdp_df["cumulative_growth"] = (gdp_df["gdp_growth"] + 1).cumprod()

gdp_df["gdp_cumulative_growth"] = gdp_df["gdp_pct_change"].cumprod()

gdp_df
year gdp gdp_prev gdp_change gdp_pct_change gdp_cumulative_growth
0 1990 100 NaN NaN 1.000000 1.00
1 1991 105 100.0 5.0 1.050000 1.05
2 1992 110 105.0 5.0 1.047619 1.10
3 1993 115 110.0 5.0 1.045455 1.15
4 1994 110 115.0 -5.0 0.956522 1.10

We can see how pandas makes it easy to calculate growth and cumulative growth in time-series data.

11.2.1 Example 1: Stock Price Growth Comparisons

To illustrate cumulative growth comparisons, let’s fetch a dataset of adjusted closing prices for a handful of stocks:

from pandas import read_csv

repo_url = "https://raw.githubusercontent.com/prof-rossetti/applied-data-science-python-book"
request_url = f"{repo_url}/main/docs/data/yahooquery/multistock_prices_pivot.csv"

prices_pivot = read_csv(request_url)
prices_pivot = prices_pivot.round(2)
prices_pivot.set_index("date", inplace=True)
prices_pivot.head()
AAPL AMZN BAC GOOGL JPM META MSFT NFLX NVDA SPY
date
2023-01-03 122.88 85.82 30.49 88.28 123.12 123.56 232.51 29.50 14.27 363.23
2023-01-04 124.14 85.14 31.06 87.25 124.27 126.16 222.34 30.94 14.70 366.04
2023-01-05 122.83 83.12 30.99 85.39 124.24 125.74 215.75 30.97 14.22 361.86
2023-01-06 127.35 86.08 31.30 86.52 126.62 128.79 218.29 31.56 14.81 370.16
2023-01-09 127.87 87.36 30.83 87.19 126.10 128.24 220.42 31.52 15.58 369.95

Each company’s stock prices are expressed using a different scale (i.e. some trade around $10 a share, while others trade around $100 a share). In order to compare their performance, we need to normalize their prices. We can do this by calculating the percent change for each stock. We calculate percent change relative to that stock’s initial price. So a growth of 1 means 100% (i.e. the stocks’ initial value).

import warnings
warnings.filterwarnings("ignore") # suppress warning messages

all_symbols = prices_pivot.columns.tolist()
growth = prices_pivot.copy()

for symbol in all_symbols:
    growth_col = f"{symbol}_growth"

    # calculate relative growth:
    growth[growth_col] = (growth[symbol].pct_change(periods=1) + 1).cumprod()

    # fill in null value resulting from there being no previous period:
    growth[growth_col].iloc[0] = 1

growth_cols = [f"{symbol}_growth" for symbol in all_symbols]

growth = growth[growth_cols]
growth = growth.round(2)
growth.head()
AAPL_growth AMZN_growth BAC_growth GOOGL_growth JPM_growth META_growth MSFT_growth NFLX_growth NVDA_growth SPY_growth
date
2023-01-03 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
2023-01-04 1.01 0.99 1.02 0.99 1.01 1.02 0.96 1.05 1.03 1.01
2023-01-05 1.00 0.97 1.02 0.97 1.01 1.02 0.93 1.05 1.00 1.00
2023-01-06 1.04 1.00 1.03 0.98 1.03 1.04 0.94 1.07 1.04 1.02
2023-01-09 1.04 1.02 1.01 0.99 1.02 1.04 0.95 1.07 1.09 1.02

Display a line chart of growth for each company, for visual comparison:

import plotly.express as px

px.line(growth, y=growth_cols, title=f"Stock Returns vs Market", height=500)

Analysis Questions:

  • Which company experienced the highest growth? The lowest?
  • Which companies out-performed the market (i.e. grew more than the S&P)? Underperformed the market?