QuestionQ15

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

The following code fragment produces an error:

@F.udf(T.IntegerType())  
def simple_udf(t: str) -> str:  
    return answer * 3.14159  

Which code fragment should be used instead?

Explanation

Multiplying a numeric value by 3.14159 produces a floating-point result. A PySpark UDF’s declared returnType specifies its Spark SQL output type, so this result should use T.DoubleType() and a Python float return annotation. The function must also multiply its defined parameter, t, rather than the undefined name answer.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!