滚动应用于熊猫节目" TypeError:只有长度为1的数组可以转换为Python标量"

时间:2016-09-18 11:46:45

标签: python arrays function pandas median

数据框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)

显示的错误是:

  

文件" pandas \ algos.pyx",第1831行,pandas.algos.roll_generic(pandas \ algos.c:51581)

     

TypeError:只能将length-1数组转换为Python标量

我使用Pandas 0.18.1和python 2.7.12

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

xlambda x: (x ...的问题输出为numpy array,因此如果仅使用test = lambda x: x numpy数组,则无法将每行转换为标量值。我认为你只需要返回标量值,例如使用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