nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #31

Which of the following code blocks returns a DataFrame containing a column dayOfYear, an integer representation of the day of the year from column openDate from DataFrame storesDF? Note that column…

The correct answer is C. storesDF.withColumn("dayOfYear", dayofyear(col("openDate"))). The dayofyear() function is a built-in PySpark SQL function that extracts the day-of-year (1–366) from a Date or Timestamp column. Choice C correctly imports and applies it: storesDF.withColumn("dayOfYear", dayofyear(col("openDate"))). Choice B has invalid syntax (get dayofyear…

Perform Data Transformations using Spark DataFrame API

Question

Which of the following code blocks returns a DataFrame containing a column dayOfYear, an integer representation of the day of the year from column openDate from DataFrame storesDF? 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. A sample of storesDF is displayed below:

Options

  • A(storesDF.withColumn("openTimestamp", col("openDate").cast("Timestamp"))
  • BstoresDF.withColumn("dayOfYear", get dayofyear(col("openDate")))
  • CstoresDF.withColumn("dayOfYear", dayofyear(col("openDate")))
  • D(storesDF.withColumn("openDateFormat", col("openDate").cast("Date"))
  • EstoresDF.withColumn("dayOfYear", substr(col("openDate"), 4, 6))

How the community answered

(38 responses)
  • A
    5% (2)
  • C
    84% (32)
  • D
    8% (3)
  • E
    3% (1)

Explanation

The dayofyear() function is a built-in PySpark SQL function that extracts the day-of-year (1–366) from a Date or Timestamp column. Choice C correctly imports and applies it: storesDF.withColumn("dayOfYear", dayofyear(col("openDate"))). Choice B has invalid syntax (get dayofyear is not a real function). Choices A and D are incomplete - they create intermediate cast columns (Timestamp and Date respectively) but never produce the required dayOfYear column. Choice E misuses substr(), which operates on string data and would not extract a meaningful day-of-year from a numeric epoch value.

Topics

#Spark DataFrame API#Date and Time Functions#Column Transformations#Epoch Time Handling

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice