使用 pandas 查找自滚动高点以来的周期数 [英] Using Pandas To Find the Number Of Periods Since the Rolling High

查看:72
本文介绍了使用 pandas 查找自滚动高点以来的周期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Pandas中使用rolling_max函数:

I am using the rolling_max function in Pandas:

http://pandas.pydata. org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments

我如何找到自价格高点以来的期数?

How would I find the number of periods since the price was at this high?

推荐答案

开始于:

>>> ts
A    10
B    10
C    -5
D   -15
E    -9
F    -8
G   -13
H    -9
I   -15
J   -21
dtype: int64

您可以这样做:

>>> rmlag = lambda xs: np.argmax(xs[::-1])
>>> pd.rolling_apply(ts, func=rmlag, window=3, min_periods=0).astype(int)
A    0
B    0
C    1
D    2
E    2
F    0
G    1
H    2
I    1
J    2
dtype: int64

与原始系列和滚动最大值并置:

concateneted with original series and rolling max values:

   value  roll-max  rm-lag
A     10        10       0
B     10        10       0
C     -5        10       1
D    -15        10       2
E     -9        -5       2
F     -8        -8       0
G    -13        -8       1
H     -9        -8       2
I    -15        -9       1
J    -21        -9       2

[10 rows x 3 columns]

这篇关于使用 pandas 查找自滚动高点以来的周期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆