HELP : Attribute error when trying to apply lead-lag corr on pd series

Compute the lead-lag correlation matrix

lags = range(-30, 31)
corr_coeffs = np.zeros((len(tickers), len(tickers), len(lags)))
for i, col1 in enumerate(returns.columns):
for j, col2 in enumerate(returns.columns):
for k, lag in enumerate(lags):
corr_coeffs[i, j, k] = returns[col1].corr(returns[col2].shift(lag))

AttributeError: ‘float’ object has no attribute ‘shape’

Can anyone pls shed some light? Confirm the returns data is complete , no N/As no missing values, and stored in float64 in pd series.

Hi @fleer_Winwin ,

This error generally occurs when we try to call a NumPy method on a non-NumPy object.
But you confirmed that the ‘returns’ data is complete with no missing values and stored as a float64 data type in a Pandas series, so we could not say exactly where the issue is coming from.

Could you please provide the complete code snippet to reproduce and provide a solution here.

Thanks.

Hi, thanks for the reply. Please refer to the below full code : import pandas as pd
import numpy as np
import eikon as ek
import matplotlib.pyplot as plt
from datetime import datetime
import seaborn as sns

Download data from Eikon

start_date = ‘2021-01-01’
end_date = datetime.today().strftime(‘%Y-%m-%d’)

tickers = {‘Ron92’:‘GL92-SIN’, ‘Sing Naphtha’:‘NAF-SIN’, ‘Sing JetKero’:‘JET-SIN’,‘Sing GO 10ppm’:‘GO10-SIN’,‘FO 380’:‘FO380-SIN’}

dfs = []
for ticker, code in tickers.items():
data = ek.get_timeseries(code, fields=[‘CLOSE’], interval=‘daily’, start_date=start_date, end_date=end_date)
if data is not None:
df = pd.DataFrame(data)
df.columns = [ticker]
dfs.append(df)
df = pd.concat(dfs, axis=1)

#Calculate the returns
returns = df.pct_change().dropna()
returns_corr=returns.corr().round(3)
print(returns.head())
print(returns_corr)
print(df.info())

#Generate heatmap using searbon’s heatmap
plt.figure(figsize=(6,6))
sns.heatmap(returns_corr,annot=True,cmap=‘coolwarm’)
plt.title(‘Correlations among Singapore Products’)
plt.xticks(rotation=45)
plt.show()

Compute the lead-lag correlation matrix

lags = range(-30, 31)
corr_coeffs = np.zeros((len(tickers), len(tickers), len(lags)))
for i, col1 in enumerate(returns.columns):
for j, col2 in enumerate(returns.columns):
for k, lag in enumerate(lags):
corr_coeffs[i, j, k] = returns[col1].corr(returns[col2].shift(lag))