我想計算正數的標準偏差。大熊貓滾動使用哪裏休息
np.random.seed(42)
d = pd.Series(data = np.random.randn(1000))
,如果我做到以下幾點:
(d.where(d>0))[-100:].std(skipna=True)
我得到0.56972171400539895
但如果我嘗試給我們的滾動功能,我只得到nan
:
(d.where(d>0)).rolling(100).std(skipna=True).iloc[-1]
有沒有一種辦法使滾動功能工作? 我已經測試過python 3.5和pandas 0.20
是否有一個特定的原因,你爲什麼使用'where'而不是布爾索引?例如,對於3的窗口,'[1,-1,2,-2,3,-3,4,-4]'你的標準偏差是多少? std([1,2,3])(前三個正數)或std([1,2])(前三個元素的正數)。 – ayhan
我將它用於時間序列(因此是滾動功能)。因此,在你的例子中,對於索引-1,3的窗口將看std([nan,4,nan]),而索引0則看std([1,nan,2])。 –