我想计算股票的每日回报

发布于 2025-01-19 00:40:53 字数 353 浏览 2 评论 0原文

    Closing_Date    AAL_Close   AAPL_Close
665 2015-01-12  49.580002   27.312500
666 2015-01-13  50.400002   27.555000
667 2015-01-14  49.410000   27.450001
668 2015-01-15  49.410000   26.705000
669 2015-01-16  49.810001   26.497499

我想将今天的股票价格除以昨天的股价。第一行将导致NAN或空。它正在计算每日回报。

我想将今天的股票价格除以昨天的股价。第一行将导致NAN或空。它正在计算每日回报。

    Closing_Date    AAL_Close   AAPL_Close
665 2015-01-12  49.580002   27.312500
666 2015-01-13  50.400002   27.555000
667 2015-01-14  49.410000   27.450001
668 2015-01-15  49.410000   26.705000
669 2015-01-16  49.810001   26.497499

I want to divide stock price of today by stock price of yesterday . The first row will result in Nan or empty . It is calculating for daily returns .

I want to divide stock price of today by stock price of yesterday . The first row will result in Nan or empty . It is calculating for daily returns .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2025-01-26 00:40:53

假设您有所有连续的日期:

df['Closing_Date'] = pd.to_datetime(df['Closing_Date'])
df2 = df.set_index('Closing_Date')

df2.div(df2.shift(1))

或者如果您真的想依靠上一个日期:

df['Closing_Date'] = pd.to_datetime(df['Closing_Date'])
df2 = df.set_index('Closing_Date')

df2.div(df2.reindex(df2.index-pd.Timedelta('1d')).values)

输出:

              AAL_Close  AAPL_Close
Closing_Date                       
2015-01-12          NaN         NaN
2015-01-13     1.016539    1.008879
2015-01-14     0.980357    0.996189
2015-01-15     1.000000    0.972860
2015-01-16     1.008096    0.992230

Assuming that you have all consecutive dates:

df['Closing_Date'] = pd.to_datetime(df['Closing_Date'])
df2 = df.set_index('Closing_Date')

df2.div(df2.shift(1))

Or if you really want to rely on the previous date:

df['Closing_Date'] = pd.to_datetime(df['Closing_Date'])
df2 = df.set_index('Closing_Date')

df2.div(df2.reindex(df2.index-pd.Timedelta('1d')).values)

output:

              AAL_Close  AAPL_Close
Closing_Date                       
2015-01-12          NaN         NaN
2015-01-13     1.016539    1.008879
2015-01-14     0.980357    0.996189
2015-01-15     1.000000    0.972860
2015-01-16     1.008096    0.992230
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文