You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行ADF平稳性检验函数报can only concatenate str (not "tuple") to str错误如何解决

ADF平稳性检验函数报错修复方案

错误根因

  • 报错"can only concatenate str (not "tuple") to str"的核心原因是循环打印语句中错误拼接了result对象:adfuller()返回的是包含多个检验结果的完整元组,你在循环中已经通过zip(result, label)取出了单个结果值value,但打印时仍然拼接了整个元组result,导致类型不匹配。

其他需要修复的问题

  • 循环中value是数值类型,需要转成字符串再拼接,推荐使用f-string格式化输出更稳妥
  • 原代码存在多处拼写错误:函数名chek应为check、statestic应为statistic、legs应为lags、evedincee应为evidence、nun stationrary应为non-stationary
  • 原检验逻辑描述错误:ADF检验的原假设为「序列存在单位根、非平稳」,p值≤0.05是拒绝原假设,原描述逻辑写反。

修复后完整代码

# 需提前导入依赖
from statsmodels.tsa.stattools import adfuller

def check_stationary(x):
    result = adfuller(x)
    labels = ['ADF statistic test', 'p value', 'num of lags', 'num of observation']
    for value, label in zip(result, labels):
        print(f"{label}: {value}")
    if result[1] <= 0.05:
        print('有充分证据拒绝原假设')
        print('序列不存在单位根')
        print('序列为平稳序列')
    else:
        print('没有充分证据拒绝原假设')
        print('序列存在单位根')
        print('序列为非平稳序列')

内容的提问来源于stack exchange,提问作者arminius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 04:24:03