[英]Pandas rolling function bug?
I have the following few samples of data:我有以下几个数据样本:
close
date
2018-11-13 192.23
2018-11-12 194.17
2018-11-09 204.47
2018-11-08 208.49
2018-11-07 209.95
2018-11-06 203.77
2018-11-05 201.59
2018-11-02 207.48
2018-11-01 222.22
2018-10-31 218.86
2018-10-30 213.30
2018-10-29 212.24
2018-10-26 216.30
2018-10-25 219.80
2018-10-24 215.09
...
Using this window,使用这个 window,
resampleStr = '2D'
and this code:和这段代码:
res = pd.concat([
df['close'].rolling(wind).apply(lambda x : (x[-1] - x[-0]) / x[-1]).reset_index(),
df.reset_index()['date'].shift(-wind).rename('T-' + resampleStr),
df.reset_index()['close'].rename('today'),
df.reset_index()['close'].shift(-wind).rename('T-' + resampleStr)
],
axis=1
)
res = res.dropna()
print(res)
I get this (partial) result.我得到这个(部分)结果。 How can this be?
怎么会这样? For example, the first roll,
(194.17 - 208.49) / 208.49 = -0.06869
, and yet the result shows -0.009991
?例如,第一卷,
(194.17 - 208.49) / 208.49 = -0.06869
,但结果显示-0.009991
?
date close T-2D today T-2D
1 2018-11-12 -0.009991 2018-11-08 194.17 208.49
2 2018-11-09 -0.050374 2018-11-07 204.47 209.95
3 2018-11-08 -0.019282 2018-11-06 208.49 203.77
4 2018-11-07 -0.006954 2018-11-05 209.95 201.59
5 2018-11-06 0.030328 2018-11-02 203.77 207.48
6 2018-11-05 0.010814 2018-11-01 201.59 222.22
7 2018-11-02 -0.028388 2018-10-31 207.48 218.86
8 2018-11-01 -0.066331 2018-10-30 222.22 213.30
9 2018-10-31 0.015352 2018-10-29 218.86 212.24
10 2018-10-30 0.026067 2018-10-26 213.30 216.30
11 2018-10-29 0.004994 2018-10-25 212.24 219.80
12 2018-10-26 -0.018770 2018-10-24 216.30 215.09
13 2018-10-25 -0.015924 2018-10-23 219.80 222.73
14 2018-10-24 0.021898 2018-10-22 215.09 220.65
EDIT 1编辑 1
Inside the lambda
, as per rafaelc
suggestion, I did a lambda x: print(x) or
then the rest of the code.在
lambda
内部,根据rafaelc
的建议,我执行了lambda x: print(x) or
代码的 rest。 It prints out these (partial) values.它打印出这些(部分)值。 It is not using the
window
?!!它没有使用
window
?!! WTF?什么鬼?
2018-11-13 192.23
2018-11-12 194.17
dtype: float64
date
2018-11-12 194.17
2018-11-09 204.47
dtype: float64
date
2018-11-09 204.47
2018-11-08 208.49
dtype: float64
date
2018-11-08 208.49
2018-11-07 209.95
dtype: float64
date
2018-11-07 209.95
2018-11-06 203.77
dtype: float64
date
2018-11-06 203.77
2018-11-05 201.59
dtype: float64
date
2018-11-05 201.59
2018-11-02 207.48
dtype: float64
date
2018-11-02 207.48
2018-11-01 222.22
dtype: float64
date
2018-11-01 222.22
2018-10-31 218.86
dtype: float64
date
2018-10-31 218.86
2018-10-30 213.30
dtype: float64
date
2018-10-30 213.30
2018-10-29 212.24
dtype: float64
date
2018-10-29 212.24
2018-10-26 216.30
dtype: float64
date
2018-10-26 216.3
2018-10-25 219.8
dtype: float64
date
2018-10-25 219.80
2018-10-24 215.09
dtype: float64
The values for 194.17
and 208.49
are for 2018-11-12
and 2018-11-09
respectively. 194.17
和208.49
的值分别适用于2018-11-12
和2018-11-09
。 They are never part of a 2-day window, which is what you defined.它们绝不是您定义的 2 天 window 的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.