英文:
Compute percentage change by increasing window size up to period
问题
如果我有这个系列:
s = pd.Series([90, 91, 85, 95])
如果我使用周期为2来计算百分比变化,结果如下:
s.pct_change(periods=2)
结果将是:
0 NaN
1 NaN
2 -0.055556
3 0.043956
dtype: float64
但假设我有几年的数据,我想计算年度滚动回报率,使用 pct_change(365)
。然后我会失去一年的数据(在这种情况下,我有365个NaN值,然后才有一个有效的观测值)。
我想要的是从1开始,然后逐渐增加,直到达到指定的periods
,而不是失去一年的数据。
换句话说,我想要的输出是:
0 NaN
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
或者
0 0
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
这是否可能?我知道pandas.DataFrame.rolling基本具有此功能,使用参数min_periods
,但我找不到类似的内容pandas.DataFrame.pct_change。
英文:
Say I have this series
s = pd.Series([90, 91, 85, 95])
If I compute the percentage change with a period of 2 I get
s.pct_change(periods=2)
0 NaN
1 NaN
2 -0.055556
3 0.043956
dtype: float64
But say I have a couple of years of data and I want to compute the year over year (YoY) rolling returns, with pct_change(365)
. Then I would loose a year's worth of data (in the sense that I have 365 NaN values before I have a valid observation).
I would instead like to compromise by starting as if I had a period of 1, 2, then 3 and so on up until I reach the specified periods
.
In other words, I would like an output of the likes of
0 NaN
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
or
0 0
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
Is this possible? I know that pandas.DataFrame.rolling has basically this functionality with the argument min_periods
, but I couldn't find something similar for pandas.DataFrame.pct_change.
答案1
得分: 2
你可以使用 [tag:numpy] 及其 sliding_window_view
:
from numpy.lib.stride_tricks import sliding_window_view as swv
N = 2
v = swv(np.pad(s, (N, 0), constant_values=s.iloc[0]), N+1)
out = pd.Series((v[:, -1]-v[:, 0])/v[:, 0], index=s.index)
输出:
0 0.000000
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
使用 pandas,它看起来像是 rolling
,其中你使用窗口的第一个和最后一个值:
N = 2
s.rolling(N+1, min_periods=0).apply(lambda x: (x.iloc[-1]-x.iloc[0])/x.iloc[0])
输出:
0 0.000000
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
英文:
You can use [tag:numpy] and its sliding_window_view
:
from numpy.lib.stride_tricks import sliding_window_view as swv
N = 2
v = swv(np.pad(s, (N, 0), constant_values=s.iloc[0]), N+1)
out = pd.Series((v[:, -1]-v[:, 0])/v[:, 0], index=s.index)
Output:
0 0.000000
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
With pandas, it looks like a rolling
in which you use the first and last value of the window:
N = 2
s.rolling(N+1, min_periods=0).apply(lambda x: (x.iloc[-1]-x.iloc[0])/x.iloc[0])
Output:
0 0.000000
1 0.011111
2 -0.055556
3 0.043956
dtype: float64
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论