我的功能:
def f(x):
print(len(x))
return
test.set_index('exchTstamp',inplace=True)
test['fit_x'].rolling('1.0S').apply(lambda x: f(list(x)))
在时间索引的数据帧上运行以下代码时,出现以下错误:
> TypeError Traceback (most recent call
> last) <ipython-input-151-4de6334ec332> in <module>()
> ----> 1 g=testTbt['fit_x'].rolling('1.0S').apply(lambda x: f(list(x)))
>
> /usr/lib64/python2.7/site-packages/pandas/core/window.pyc in
> apply(self, func, raw, args, kwargs) 1701 def apply(self, func,
> raw=None, args=(), kwargs={}): 1702 return super(Rolling,
> self).apply(
> -> 1703 func, raw=raw, args=args, kwargs=kwargs) 1704 1705 @Substitution(name='rolling')
>
> /usr/lib64/python2.7/site-packages/pandas/core/window.pyc in
> apply(self, func, raw, args, kwargs) 1010 1011 return
> self._apply(f, func, args=args, kwargs=kwargs,
> -> 1012 center=False, raw=raw) 1013 1014 def sum(self, *args, **kwargs):
>
> /usr/lib64/python2.7/site-packages/pandas/core/window.pyc in
> _apply(self, func, name, window, center, check_minp, **kwargs)
> 878 result = np.apply_along_axis(calc, self.axis, values)
> 879 else:
> --> 880 result = calc(values)
> 881
> 882 if center:
>
> /usr/lib64/python2.7/site-packages/pandas/core/window.pyc in calc(x)
> 872 def calc(x):
> 873 return func(x, window, min_periods=self.min_periods,
> --> 874 closed=self.closed)
> 875
> 876 with np.errstate(all='ignore'):
>
> /usr/lib64/python2.7/site-packages/pandas/core/window.pyc in f(arg,
> window, min_periods, closed) 1007 return
> libwindow.roll_generic( 1008 arg, window, minp,
> indexi,
> -> 1009 closed, offset, func, raw, args, kwargs) 1010 1011 return self._apply(f, func, args=args,
> kwargs=kwargs,
>
> pandas/_libs/window.pyx in pandas._libs.window.roll_generic()
>
> TypeError: a float is required
我基本上想在滚动基础上进行一些计算,例如计算指数移动平均值。请让我知道我要去哪里了。
可以找到数据here
答案 0 :(得分:1)
似乎您想使用函数f
返回一个值。 apply
与map
类似,它需要返回一个值。