DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #115
Which of the following code blocks returns a DataFrame containing a column openDateString, a string representation of Java's SimpleDateFormat? Note that column openDate is of type integer and…
The correct answer is A. storesDF.withColumn("openDatestring", from unixtime(col("openDate"), "EEEE, MMM d, yyyy. The correct PySpark function for converting a UNIX timestamp (seconds since epoch) to a human-readable string is from_unixtime(). The Java SimpleDateFormat pattern for the example 'Sunday, Dec 4, 2008 1:05 pm' is 'EEEE, MMM d, yyyy h:mm a'. The correct approach is…
Question
Which of the following code blocks returns a DataFrame containing a column openDateString, a string representation of Java's SimpleDateFormat? Note that column openDate is of type integer and represents a date in the UNIX epoch format - the number of seconds since midnight on January 1st, 1970. An example of Java's SimpleDateFormat is "Sunday, Dec 4, 2008 1:05 pm". A sample of storesDF is displayed below:
Options
- AstoresDF.withColumn("openDatestring", from unixtime(col("openDate"), "EEEE, MMM d, yyyy
- BstoresDF.withColumn("openDateString", from_unixtime(col("openDate"), "EEEE, MMM d, yyyy
- CstoresDF.withColumn("openDateString", date(col("openDate"), "EEEE, MMM d, yyyy h:mm a"))
- DstoresDF.newColumn(col("openDateString"), from_unixtime("openDate", "EEEE, MMM d, yyyy
- EstoresDF.withColumn("openDateString", date(col("openDate"), "EEEE, MMM d, yyyy h:mm a",
How the community answered
(21 responses)- A90% (19)
- C5% (1)
- D5% (1)
Explanation
The correct PySpark function for converting a UNIX timestamp (seconds since epoch) to a human-readable string is from_unixtime(). The Java SimpleDateFormat pattern for the example 'Sunday, Dec 4, 2008 1:05 pm' is 'EEEE, MMM d, yyyy h:mm a'. The correct approach is storesDF.withColumn('openDateString', from_unixtime(col('openDate'), 'EEEE, MMM d, yyyy h:mm a')). Choice A is marked correct here. Choice C and E use a non-existent date() function. Choice D uses a non-existent newColumn() method. Note: in a live environment, the underscore form from_unixtime (not from unixtime) is the valid import from pyspark.sql.functions.
Topics
Community Discussion
No community discussion yet for this question.