簡體   English   中英

Pandas 滾動 function 錯誤?

[英]Pandas rolling function bug?

我有以下幾個數據樣本:

             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
...

使用這個 window,

resampleStr = '2D'

和這段代碼:

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)

我得到這個(部分)結果。 怎么會這樣? 例如,第一卷, (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

編輯 1

lambda內部,根據rafaelc的建議,我執行了lambda x: print(x) or代碼的 rest。 它打印出這些(部分)值。 它沒有使用window ?!! 什么鬼?

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

194.17208.49的值分別適用於2018-11-122018-11-09 它們絕不是您定義的 2 天 window 的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM