
Answer-first summary for fast verification
Answer: df.withColumn('event_time', to_timestamp('event_time', 'yyyy-MM-dd HH:mm:ss'))
The correct answer is A because it correctly uses the `to_timestamp` function to cast the `event_time` column to a timestamp, specifying the format to ensure correct parsing.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
Given a DataFrame df with a column event_time in string format, how would you cast this column to a timestamp using Spark? Provide the code snippet.
A
df.withColumn('event_time', to_timestamp('event_time', 'yyyy-MM-dd HH:mm:ss'))
B
df.select(to_timestamp('event_time', 'yyyy-MM-dd HH:mm:ss'))
C
df.withColumn('event_time', col('event_time').cast('timestamp'))
D
df.select(col('event_time').cast('timestamp'))
No comments yet.