Skip to content
Advertisement

Python/Pandas time series correlation on values vs differences

I am familiar with Pandas Series corr function to compute the correlation between two Series, so example:

import pandas as pd
import numpy as np

A = pd.Series(np.random.randint(1,101,50))
B = pd.Series(np.random.randint(1,101,50))

A.corr(B)

This willl compute the correlation in the VALUES of the two series, but if I’m working with a Time Series, I might want to compute teh correlation on changes (absolute changes or percentage changes and over 1d, 1w, 1m, etc).

Some of the statistical software can do that quite easily and of course I could create a series with the daily, weekly, changes and then run the same function, but I wondered it there was a more Pythonic way to do this?

Advertisement

Answer

I guess the more pythonic way, through pandas, would be to use df.pct_change():

Suppose A and B are time series:

A.pct_change().corr(B.pct_change())
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement