DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #23
The code block shown below contains an error. The code block is intended to create a Python UDF assessPerformanceUDF() using the integer-returning Python function assessPerformance() and apply it to…
The correct answer is D. The return type of the assessPerformanceUDF() is not specified in the udf() operation. When creating a UDF with udf(), PySpark requires an explicit return type so it knows how to serialize the output back into a Spark column. Without specifying the return type (e.g., udf(assessPerformance, IntegerType())), Spark defaults to StringType(), which may cause incorrect…
Question
The code block shown below contains an error. The code block is intended to create a Python UDF assessPerformanceUDF() using the integer-returning Python function assessPerformance() and apply it to column customerSatisfaction in DataFrame storesDF. Identify the error. Code block:
assessPerformanceUDF - udf(assessPerformance) storesDF.withColumn("result", assessPerformanceUDF(col("customerSatisfaction")))
Options
- AThe assessPerformance() operation is not properly registered as a UDF.
- BThe withColumn() operation is not appropriate here - UDFs should be applied by iterating over
- CUDFs can only be applied vie SQL and not through the DataFrame API.
- DThe return type of the assessPerformanceUDF() is not specified in the udf() operation.
- EThe assessPerformance() operation should be used on column customerSatisfaction rather than
How the community answered
(39 responses)- A3% (1)
- B5% (2)
- C3% (1)
- D87% (34)
- E3% (1)
Explanation
When creating a UDF with udf(), PySpark requires an explicit return type so it knows how to serialize the output back into a Spark column. Without specifying the return type (e.g., udf(assessPerformance, IntegerType())), Spark defaults to StringType(), which may cause incorrect results or type mismatches. The line assessPerformanceUDF = udf(assessPerformance) is missing the return type argument, which is the error.
Topics
Community Discussion
No community discussion yet for this question.