Spotfire初学者,如何使用Python数据函数?

2024-09-27 19:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

Spotfire初学者在这里。 我一直在尝试使用Python数据函数向表中添加一个计算列。 从IDE运行的Python代码可以正常工作,但在Python数据函数内部运行时,会抛出一个错误。 我读到Spotfire到Python的专栏被映射为Pandas系列数据,但我不确定如何利用这些信息并进行必要的修改,以便代码在Spotfire中运行。 我想要一些指导,或者如果可能的话给我指出正确的方向

使用Spotfire Analyst 10.10。 输入:我的表中数据类型为整数的列。 输出:应为数据类型字符串的列

尝试使用以下代码迭代列的值

Code:
    def affordability(value):
        if value>1000.00:
            print ("Price is more than 1000")
        elif 500.00<=value<=999.99:
            print ("Price is in Mid range")
        elif 0.00<=value<=499.99:
            print ("Price is affordable")       
    #End of the user defined funcion
    
    inter=[]
    doub_profit=profit*2          #profit is the input variable

    for i in doub_profit:
        situation=affordability(i)
        inter.append(situation)
    #End of for loop

    end_res=inter               #end_res is the output variable

错误消息:

Error executing Python script:

    spotfire.sbdf.SBDFError: cannot determine type for list; all values are missing
    
    Traceback (most recent call last):
      File "data_function.py", line 366, in _write_outputs
        output.write(self.globals, self.debug)
      File "data_function.py", line 149, in write
        sbdf.export_data(globals_dict[self.name], self.file, default_column_name=self.name)
      File "sbdf.py", line 163, in export_data
        columns, column_names, column_types, table_metadata, column_metadata = _export_columnize_data(obj,
      File "sbdf.py", line 247, in _export_columnize_data
        column_types = {default_column_name: _ValueTypeId.infer_from_type(list(obj), "list")}
      File "sbdf.py", line 986, in infer_from_type
        raise SBDFError("cannot determine type for %s; all values are missing" % value_description)
Standard Ouput: 
The Output I want is present here.

Debug log:
debug: start evaluate
debug: reading 1 input variables
debug: assigning column 'profit' from file tmpdir\dfpythondf_artifact_input_sgoua4gzlbjtmp.sbdf
debug: read 9426 rows 1 columns
debug: table metadata: 
 {}

debug: column metadata: 
 {'Profit': {}}

debug: done reading 1 input variables
debug: executing script
debug: --- script ---
def affordability(value):
    if value>1000.00:
        print ("Price is more than 1000")
    elif 500.00<=value<=999.99:
        print ("Price is in Mid range")
    elif 0.00<=value<=499.99:
        print ("Price is affordable")       
#End of the user defined funcion

inter=[]
doub_profit=profit*2
for i in doub_profit:
    situation=affordability(i)
    inter.append(situation)
#End of for loop

end_res=inter

debug: --- script ---
debug: analytic_type is 'script'
debug: done executing script
debug: writing 1 output variables
debug: returning 'end_res' as file tmpdir\dfpythondf_artifact_output_d5cbcjvjbsktmp.sbdf
debug: done writing 1 output variables
    
       at Spotfire.Dxp.Data.DataFunctions.Executors.LocalPythonFunctionClient.<RunFunction>d__8.MoveNext()
       at Spotfire.Dxp.Data.DataFunctions.Executors.PythonScriptExecutor.<ExecuteFunction>d__11.MoveNext()
       at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.<ExecuteFunction>d__8.MoveNext()

注意:这是我在stackoverflow中的第一篇文章,非常感谢有关于这个主题的代码示例(Spotfire数据函数)的参考资料


Tags: indebugforoutputdataisvaluescript
1条回答
网友
1楼 · 发布于 2024-09-27 19:25:02

当值/列表/数据框中没有值时,会发生此错误。这意味着Spotfire无法确定返回的数据类型,即整数、字符串等,以创建必要的列或表作为输出。我想尝试的两件事是:

  1. 通过在脚本末尾添加打印(end_res)来检查end_res值。这将出现在错误消息中,因此您可以查看它是否已填充。添加打印语句是在出现错误时进行一些检查的快速方法

  2. 我通常会在返回之前将所有列表转换为数据帧。我不知道列表是否是问题的一部分,但我会尝试以下方法:

    end_res=pd.DataFrame(inter,columns=[“可承受性])

对于上面的示例,您实际上可能只需要在Spotfire中使用CASE语句,而无需调用Python。如果通过“数据”菜单将计算列添加到数据表中,然后使用CASE表达式,则可以轻松复制上述函数中的行为。以下是有关计算列的快速指南:

https://www.youtube.com/watch?v=0GpB1F4XEFI

在Python上有很多很好的YouTube Dr Spotfire会话,例如: https://www.youtube.com/watch?v=MXhO3yG6dUghttps://www.youtube.com/watch?v=EDOIZACQmhw这可能会有所帮助

我还写了一些关于做相关分析和变量重要性等任务的例子:https://community.tibco.com/wiki/performing-correlation-analysis-using-python-data-functions-tibco-spotfire

值得在YouTube上关注Spotfire博士。这里还有Spotfire启用中心: https://community.tibco.com/wiki/spotfire-enablement-hub

相关问题 更多 >

    热门问题