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

Spotfire中Python数据函数创建计算列遇Output变量未定义错误

Spotfire Python数据函数报错:Output variable 'passage' was not defined 解决方法

报错原因

Spotfire的Python数据函数要求输出变量必须定义在全局作用域中,原脚本里的passage仅在calculate_passage函数内部声明,全局命名空间中不存在该变量,导致Spotfire无法识别输出。

解决方法

方法1:在全局作用域调用函数并赋值给输出变量

修改脚本,在函数定义后添加全局调用代码,将函数返回值赋值给passage变量:

import pandas as pd

def calculate_passage(df, batch_col, date_col):
    df['RowIndex'] = df.groupby(batch_col).cumcount() + 1
    df['Passage'] = 0

    for batch, batch_df in df.groupby(batch_col):
        passage = 1
        same_date_count = 0
        prev_date = None

        for index, row in batch_df.iterrows():
            if row['RowIndex'] <= 6:
                passage = 1
            elif row['RowIndex'] <= 10:
                passage = 2
            else:
                if row[date_col] == prev_date:
                    same_date_count += 1
                else:
                    same_date_count = 0

                if same_date_count == 1:
                    passage += 1

            df.loc[index, 'Passage'] = passage
            prev_date = row[date_col]

    df.drop(columns=['RowIndex'], inplace=True)
    return df[['Passage']] 

# 关键:全局作用域中调用函数,赋值给输出变量passage
# 替换input_df、批次列名、日期列名为你在Spotfire中配置的实际输入参数
passage = calculate_passage(input_df, "BatchColumn", "DateColumn")

方法2:移除函数封装,直接在全局作用域编写逻辑

如果不需要复用函数逻辑,可以直接去掉函数,将代码放在全局作用域中,确保passage变量在全局可访问:

import pandas as pd

# 使用Spotfire映射的全局输入变量(替换为你的实际输入名称)
df = input_df
batch_col = "BatchColumn"
date_col = "DateColumn"

df['RowIndex'] = df.groupby(batch_col).cumcount() + 1
df['Passage'] = 0

for batch, batch_df in df.groupby(batch_col):
    passage_val = 1
    same_date_count = 0
    prev_date = None

    for index, row in batch_df.iterrows():
        if row['RowIndex'] <= 6:
            passage_val = 1
        elif row['RowIndex'] <= 10:
            passage_val = 2
        else:
            if row[date_col] == prev_date:
                same_date_count += 1
            else:
                same_date_count = 0

            if same_date_count == 1:
                passage_val += 1

        df.loc[index, 'Passage'] = passage_val
        prev_date = row[date_col]

df.drop(columns=['RowIndex'], inplace=True)
passage = df[['Passage']]

注意事项

  • 确保全局调用时使用的输入变量名称(如input_df)与你在Spotfire数据函数配置中设置的输入参数映射名称完全一致。
  • 批次列名和日期列名要替换为你数据源中实际的列名(字符串格式)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 04:45:34