在Python中计算多证券的logreturn

2024-09-24 02:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,我已经建立了一个价格表(如图所示),我想计算各个股票的回报率。Price Table 价格数据=pd.read_excel软件(r'\价格数据.xlsx',skiprows=范围(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是我的数据帧名称。在

我面对的错误信息是:

"TypeError: ("unsupported operand type(s) for /: 'float' and >'datetime.datetime'", 'occurred at index LYB UN Equity')"

更新

我试过:

^{pr2}$

但是我遇到了一个新的错误消息:

("'float' object has no attribute 'log'", 'occurred at index LYB UN Equity')

请指教!在


Tags: to数据logfordatetimeindexnptable
1条回答
网友
1楼 · 发布于 2024-09-24 02:27:45

我想出了一个更直观的方法。在

# Calculate returns
returns = table.pct_change() # simple linear returns
log_rets = np.log(1+returns)

当计算同一数据帧中多个证券的日志回报时,这将起作用。干杯!在

相关问题 更多 >