如何在Pandas DataFrame上计算滚动累积产品

编程入门 行业动态 更新时间:2024-10-28 21:25:28
本文介绍了如何在Pandas DataFrame上计算滚动累积产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在熊猫DataFrame中有一个时间序列的收益,滚动beta和滚动alpha.如何计算DataFrame的Alpha列的滚动年度化Alpha? (我想在Excel中做= PRODUCT(1+ [trailing 12 months]]-1)

I have a time series of returns, rolling beta, and rolling alpha in a pandas DataFrame. How can I calculate a rolling annualized alpha for the alpha column of the DataFrame? (I want to do the equivalent to =PRODUCT(1+[trailing 12 months])-1 in excel)

SPX Index BBOEGEUS Index Beta Alpha 2006-07-31 0.005086 0.001910 1.177977 -0.004081 2006-08-31 0.021274 0.028854 1.167670 0.004012 2006-09-30 0.024566 0.009769 1.101618 -0.017293 2006-10-31 0.031508 0.030692 1.060355 -0.002717 2006-11-30 0.016467 0.031720 1.127585 0.013153

我很惊讶地发现熊猫没有为此内置滚动"功能,但是我希望有人可以提供一个功能,然后可以使用pd.rolling_apply将其应用于df ['Alpha']列

I was surprised to see that there was no "rolling" function built into pandas for this, but I was hoping somebody could help with a function that I can then apply to the df['Alpha'] column using pd.rolling_apply.

在此先感谢您提供的任何帮助.

Thanks in advance for any help you have to offer.

推荐答案

会这样做吗?

import pandas as pd import numpy as np # your DataFrame; df = ... pd.rolling_apply(df, 12, lambda x: np.prod(1 + x) - 1)

更多推荐

如何在Pandas DataFrame上计算滚动累积产品

本文发布于:2023-11-22 07:14:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616457.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   产品   Pandas   DataFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!