This is the same issue with #5071, but still not solved. :(
func
in GroupBy.apply(func, *args, **kwargs)[source]
have DataFrame
as an input, while
func
in Rolling.apply(func, args=(), kwargs={})
have ndarray
as an input.
Is this project still actively working to find solution?
Also, it would be better if it support parallel processing.
Code Sample
import pandas as pd
df = pd.DataFrame({'g':[1,1,1,1,2,2,2,3,3,3,3,3],
'A':[1,4,2,5,3,5,3,2,1,2,3,6],
'B':[1,5,2,3,1,5,7,3,5,2,8,4]})
print(df.groupby('g').apply(lambda x:str(type(x))))
print(df.rolling(3).apply(lambda x:str(type(x))))
g
1 <class 'pandas.core.frame.DataFrame'>
2 <class 'pandas.core.frame.DataFrame'>
3 <class 'pandas.core.frame.DataFrame'>
dtype: object
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-497-2f7f95b2a358> in <module>()
----> 1 df.rolling(3).apply(lambda x:str(type(x)))
~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
1205 @Appender(_shared_docs['apply'])
1206 def apply(self, func, args=(), kwargs={}):
-> 1207 return super(Rolling, self).apply(func, args=args, kwargs=kwargs)
1208
1209 @Substitution(name='rolling')
~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
854
855 return self._apply(f, func, args=args, kwargs=kwargs,
--> 856 center=False)
857
858 def sum(self, *args, **kwargs):
~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in _apply(self, func, name, window, center, check_minp, how, **kwargs)
797 with np.errstate(all='ignore'):
798 if values.ndim > 1:
--> 799 result = np.apply_along_axis(calc, self.axis, values)
800 else:
801 result = calc(values)
~/anaconda3/lib/python3.6/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
130 except StopIteration:
131 raise ValueError('Cannot apply_along_axis when any iteration dimensions are 0')
--> 132 res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
133
134 # build a buffer for storing evaluations of func1d.
~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in calc(x)
793 def calc(x):
794 return func(x, window, min_periods=self.min_periods,
--> 795 closed=self.closed)
796
797 with np.errstate(all='ignore'):
~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in f(arg, window, min_periods, closed)
851 minp = _use_window(min_periods, window)
852 return _window.roll_generic(arg, window, minp, indexi, closed,
--> 853 offset, func, args, kwargs)
854
855 return self._apply(f, func, args=args, kwargs=kwargs,
pandas/_libs/window.pyx in pandas._libs.window.roll_generic (pandas/_libs/window.c:36065)()
TypeError: must be real number, not str
Comment From: jreback
duplicate of #5071 opening a new issue is not helpful