私は次のシリーズを持っています。自分で定義した関数 "test"をローリングし、すぐに結果をsに更新して、 "test"の次の反復が更新されたsに基づいているようにします。パンダローリングが適用され、次の反復のためにシリーズが更新されますか?
s = pd.Series(range(5), index=pd.date_range('1/1/2000', periods=5))
s
2000-01-01 0
2000-01-02 1
2000-01-03 2
2000-01-04 3
2000-01-05 4
Freq: D, dtype: int32
私の自己定義関数は次のようになります。これは私の実際の事例の単純な例です。最初の反復で、返された変数 'update'が100に設定されていて、sを[0、1、100、3、4、...]として更新したいことがわかります。次の反復では、arr.sum()は(1 + 2 + 3)の代わりに(1 + 100 + 3)に基づいて計算されます。
def test(arr):
print(arr)
print(arr.sum())
if arr.sum()%3==0:
print('True')
update=100
else:
update=arr[-1]
return update
s=s.rolling(window=3).apply(test)
[ 0. 1. 2.]
3.0
True
[ 1. 2. 3.]
6.0
True
[ 2. 3. 4.]
9.0
True
理想的な出力:
[ 0. 1. 2.]
3.0
True
'Update s with 100'
[ 1. 100. 3.]
104
[ 100. 3. 4.]
107