1
我可以提取特定的滯後時間與此自相關值:獲取statsmodels ACF功能的輸出與熊貓滾動窗口
df.rolling(window = 10).apply(lambda x: acf(x, nlags = 5)[5]).plot()
但是由於acf
實際上是在做所有的計算,無論如何,我想計算所有結果,而不僅僅是一個。這個想法是,我可以解開這個單一的返回數組/列表成一堆列並分別繪製每一個,但不會通過acf
這麼多不必要的時間。所以,我想:
df.rolling(window = 10).apply(lambda x: list(acf(x, nlags = 5)))
這引發以下錯誤:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-63-e5f337100eb5> in <module>()
----> 1 df.rolling(window = 10).apply(lambda x: list(acf(x, nlags = 5)))
/Users/a/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
861 @Appender(_shared_docs['apply'])
862 def apply(self, func, args=(), kwargs={}):
--> 863 return super(Rolling, self).apply(func, args=args, kwargs=kwargs)
864
865 @Substitution(name='rolling')
/Users/a/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
619
620 return self._apply(f, func, args=args, kwargs=kwargs,
--> 621 center=False)
622
623 def sum(self, **kwargs):
/Users/a/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in _apply(self, func, name, window, center, check_minp, how, **kwargs)
556
557 if values.ndim > 1:
--> 558 result = np.apply_along_axis(calc, self.axis, values)
559 else:
560 result = calc(values)
/Users/a/anaconda3/lib/python3.5/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
89 outshape = asarray(arr.shape).take(indlist)
90 i.put(indlist, ind)
---> 91 res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
92 # if res is a number, then we have a smaller output array
93 if isscalar(res):
/Users/a/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in calc(x)
553
554 def calc(x):
--> 555 return func(x, window, min_periods=self.min_periods)
556
557 if values.ndim > 1:
/Users/a/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in f(arg, window, min_periods)
616 minp = _use_window(min_periods, window)
617 return algos.roll_generic(arg, window, minp, offset, func, args,
--> 618 kwargs)
619
620 return self._apply(f, func, args=args, kwargs=kwargs,
pandas/algos.pyx in pandas.algos.roll_generic (pandas/algos.c:51581)()
TypeError: a float is required
這是否意味着apply
風格與操作rolling
只能處理花車?至少對於groupby
我經常有機會返回列表或集合,但也許rolling
不那麼靈活?