Not getting same results as pct_change when doing manually

Viewed 31

My code is as follows:

import pandas as pd
from pandas_datareader import data as web
import datetime

start = datetime.datetime(2021, 1, 1)
end = datetime.datetime.today()
df = web.DataReader('goog', 'yahoo', start, end)
df['pct']= df['Close'].pct_change()

Simple enough, produces:

            High            Low             Open            Close           Volume      Adj             pct
Date                            
2020-12-31  1758.930054     1735.420044     1735.420044     1751.880005     1011900     1751.880005     NaN
2021-01-04  1760.650024     1707.849976     1757.540039     1728.239990     1901900     1728.239990     -0.013494
2021-01-05  1747.670044     1718.015015     1725.000000     1740.920044     1145300     1740.920044     0.007337
2021-01-06  1748.000000     1699.000000     1702.630005     1735.290039     2602100     1735.290039     -0.003234
2021-01-07  1788.400024     1737.050049     1740.060059     1787.250000     2265000     1787.250000     0.029943
...     ...     ...     ...     ...     ...     ...     ...
2021-08-13  2773.479980     2760.100098     2767.149902     2768.120117     628600  2768.120117     0.000119
2021-08-16  2779.810059     2723.314941     2760.000000     2778.320068     902000  2778.320068     0.003685
2021-08-17  2774.370117     2735.750000     2763.820068     2746.010010     1063600     2746.010010     -0.011629
2021-08-18  2765.879883     2728.419922     2742.310059     2731.399902     746700  2731.399902     -0.005320
2021-08-19  2748.925049     2707.120117     2709.350098     2738.270020     856623  2738.270020     0.002515

160 rows × 7 columns

So the last row says the pct is 0.002515 My objecting was to reproduce the same result without pct_change to do this i have this code

(1- (df['Close'] / df['Close'].shift(-1))).shift(1)

which produces this:

Date
2020-12-31         NaN
2021-01-04   -0.013679
2021-01-05    0.007284
2021-01-06   -0.003244
2021-01-07    0.029073
                ...   
2021-08-13    0.000119
2021-08-16    0.003671
2021-08-17   -0.011766
2021-08-18   -0.005349
2021-08-19    0.002509
Name: Close, Length: 160, dtype: float64

The last value I get is 0.002509 not 0.002515. Could you please explain why I am getting the last 2 digits off on each calulation?

1 Answers

Percent change is normally the change relative to the initial value:

(final - initial) / initial = final / initial - 1

You have the ratio relative to the final value. Try

df['Close'].shift(1) / df['Close'] - 1

By the way, you only need to shift once in your original expression as well.

Related