DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #106
The code block shown below contains an error. The code block is intended to return a new DataFrame from DataFrame storesDF where column storeId is of the type string. Identify the error. Code block…
The correct answer is C. The call to StringType should not be followed by parentheses. The code uses cast() as if it were a standalone function - cast(col('storeId'), StringType()) - but cast() is actually a method on the Column class, not a standalone function imported from pyspark.sql.functions. Additionally, StringType used as a type specifier in a cast…
Question
The code block shown below contains an error. The code block is intended to return a new DataFrame from DataFrame storesDF where column storeId is of the type string. Identify the error. Code block:
storesDF.withColumn("storeId", cast(col("storeId"), StringType()))
Options
- ACalls to withColumn() cannot create a new column of the same name on which it is operating.
- BDataFrame columns cannot be converted to a new type inside of a call to withColumn().
- CThe call to StringType should not be followed by parentheses.
- DThe column name storeId inside the col() operation should not be quoted.
- EThe cast() operation is a method in the Column class rather than a standalone function.
How the community answered
(36 responses)- A3% (1)
- B3% (1)
- C89% (32)
- D6% (2)
Explanation
The code uses cast() as if it were a standalone function - cast(col('storeId'), StringType()) - but cast() is actually a method on the Column class, not a standalone function imported from pyspark.sql.functions. Additionally, StringType used as a type specifier in a cast context should not require instantiation with parentheses. The correct syntax is: storesDF.withColumn('storeId', col('storeId').cast(StringType)) or using the string alias col('storeId').cast('string'). Answer C identifies that StringType should not be followed by parentheses in this context.
Topics
Community Discussion
No community discussion yet for this question.