here's what I have thus far - I have built a price table (as shown in the image), and I would like to calculate the log-returns of the respective tickers.
priceData = pd.read_excel(r'..\PriceData.xlsx', skiprows=range(1),
usecols = 'B:SN', index_col = 0)
priceData = priceData.drop(priceData.index[[0,1]])
priceData.index.names = ['Date']
priceData.index = priceData.index.map(pd.to_datetime)
priceData.sort_index()
# To adjust all time series data to start from 1990-01-25 to 2018-09-24
for column in priceData.columns:
if np.isnan(priceData[column].iloc[0]):
priceData = priceData.drop([column],axis=1, inplace=True)
stocks = list(table)
returns = table.apply(lambda x: np.log(x)-np.log(x.shift(1)))
*table is my dataframe name.
The error message I have faced is:
"TypeError: ("unsupported operand type(s) for /: 'float' and >'datetime.datetime'", 'occurred at index LYB UN Equity')"
I have tried with:
returns = table.apply(lambda x: np.log(x)-np.log(x.shift(1)))
But I am met with a new error message:
("'float' object has no attribute 'log'", 'occurred at index LYB UN Equity')
Please advise!
I have figured out a more intuitive way.
# Calculate returns
returns = table.pct_change() # simple linear returns
log_rets = np.log(1+returns)
This will work when calculating log returns of multiple securities in the same dataframe. Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With