Skip to main content
 首页 » 编程设计

python之在 Pandas 中滚动应用显示 "TypeError: only length-1 arrays can be converted to Python scalars"

2025年04月02日8powertoolsteam

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 ... is numpy array, 所以如果只使用 test = lambda x: x numpy array cannot be converted to scalar values per each row. 我认为你只需要返回标量值,例如使用 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