1

I can extract an autocorrelation value for a specific lag time with this:

df.rolling(window = 10).apply(lambda x: acf(x, nlags = 5)[5]).plot()

However since acf is actually doing all the calculations anyway, I'd like to get all the results calculated, not just a single one. The idea would be that I could then unpack this single returned array/list into a bunch of columns and plot each one separately but not run through acf so many unnecessary times. So I tried:

df.rolling(window = 10).apply(lambda x: list(acf(x, nlags = 5)))

This throws the following error:

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

Does this mean apply style operations with rolling can only handle floats? At least for groupby I have often had occasion to return lists or sets, but perhaps rolling is not so flexible?

helloB
  • 3,472
  • 10
  • 40
  • 87

1 Answers1

0

To plot acf results you may want to try tsaplots.plot_acf():

from statsmodels.graphics import tsaplots

tsaplots.plot_acf(x, lags = 5, alpha = 0.05)
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Jessica H N
  • 23
  • 1
  • 3