Beginner to Spotfire, How to utilise the Python Data Function?

Spotfire beginner here. I have been trying to use Python Data Function to add a calculated column to my Website Jazz Daily Internet Package. The Python code when run from the IDE is working, but when run inside the Python Data Function, it's throwing an error. I read that the column from Spotfire to Python is mapped as Pandas Series data, but I`m not sure how to make use of this information and make the necessary modifications so that the code runs inside Spotfire. I would like some guidance or if possible point me in the right direction.

Using Spotfire Analyst 10.10. Input: A Column from my table of datatype Integer. Output: Should be a Column of the datatype String.

Tried to iterate over the values of the column with the below code.

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 Message

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()

Note: This is my first post in TIBCO Community, any lead to reference material that has code samples on this topic (Spotfire Data Functions) is really appreciated.

1 Comment

How did you define the input and output in the parameters menu? would be helpful to know 

dtopalsk - May 24, 2021 - 10:44am
+ Add a Comment
Login