rolling apply in pandas shows "TypeError: only length-1 arrays can be converted to Python scalars"

rolling apply in pandas shows "TypeError: only length-1 arrays can be converted to Python scalars"

Dataframe df_implied_full 有几列,其中一列叫做 'USDZARV1Y Curncy',它只有 floats.

此代码有效:

mad                          = lambda x: np.median(np.fabs(x - np.median(x)))
df_implied_full['madtest']   = df_implied_full['USDZARV1Y Curncy'].rolling(window=60).apply(mad)

此代码无效:

test                         = lambda x: (x - np.median(x))
df_implied_full['rolltest2'] = df_implied_full['USDZARV1Y Curncy'].rolling(window=60).apply(test)

显示的错误是:

File "pandas\algos.pyx", line 1831, in pandas.algos.roll_generic (pandas\algos.c:51581)

TypeError: only length-1 arrays can be converted to Python scalars

我正在使用 Pandas 0.18.1 和 python 2.7.12

我的代码有什么问题?

lambda x: (x ...x 的问题输出是 numpy array,因此如果仅使用 test = lambda x: x numpy 数组无法转换为每一行的标量值。我认为您只需要 return 标量值,例如使用 x[0]np.median(x)。最好是使用自定义函数并测试它。

样本 window=2:

import pandas as pd
import numpy as np

df_implied_full = pd.DataFrame({'USDZARV1Y Curncy': [1.2,4.6,7.3,4.9,1.5]})
print (df_implied_full)

def test (x):
    print (x)

    #[ 1.2  4.6]
    #[ 4.6  7.3]
    #[ 7.3  4.9]
    #[ 4.9  1.5]

    print (type(x))
    #<class 'numpy.ndarray'>
    #<class 'numpy.ndarray'>
    #<class 'numpy.ndarray'>
    #<class 'numpy.ndarray'>

    #Return only first value of list
    return x[0]

mad                          = lambda x: np.median(np.fabs(x - np.median(x)))
df_implied_full['madtest']   = df_implied_full['USDZARV1Y Curncy'].rolling(window=2).apply(test)

print (df_implied_full)
   USDZARV1Y Curncy  madtest
0               1.2      NaN
1               4.6      1.2
2               7.3      4.6
3               4.9      7.3
4               1.5      4.9

def test (x):
def test (x):
    print (x)

    #[ 1.2  4.6]
    #[ 4.6  7.3]
    #[ 7.3  4.9]
    #[ 4.9  1.5]

    #Return median as scalar
    return np.median(x)

mad                          = lambda x: np.median(np.fabs(x - np.median(x)))
df_implied_full['madtest']   = df_implied_full['USDZARV1Y Curncy'].rolling(window=2).apply(test)

print (df_implied_full)
   USDZARV1Y Curncy  madtest
0               1.2      NaN
1               4.6     2.90
2               7.3     5.95
3               4.9     6.10
4               1.5     3.20