QuestionQ246

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

The following code block has an error. It is intended to return a DataFrame with a dayOfYear column containing the integer day-of-year representation of the openDate column from the storesDF DataFrame. Identify the error.

Note that openDate is an integer column representing a date in UNIX epoch format—the number of seconds since midnight on January 1st, 1970.

A sample of storesDF is shown below:

Question Image

Code block:

storesDF.withColumn("dayOfYear", dayofyear(col("openDate")))  
Explanation

Spark’s dayofyear extracts the day of the year from a date or timestamp column, and its argument may be a Column object. An integer containing Unix-epoch seconds is not itself a calendar date value, so it must be converted to a timestamp before applying dayofyear.

Learn more

Community Discussion

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