Courseiva
Develop data processingeasyMultiple ChoiceObjective-mapped

DP-203 Develop data processing Practice Question

You are building a data transformation in Azure Databricks using PySpark. The data includes a column 'timestamp' in string format 'yyyy-MM-dd HH:mm:ss'. You need to convert this to a timestamp type and extract the date part for partitioning. Which code snippet should you use?

⚠ Common exam trap

Test-takers frequently confuse `to_date` and `to_timestamp`, assuming both extract only the date, or they forget that `cast('date')` does not accept a custom format string, leading to runtime errors or null values.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

df.withColumn('date', to_date('timestamp', 'yyyy-MM-dd HH:mm:ss'))

`to_date` with the format string 'yyyy-MM-dd HH:mm:ss' converts the string column to a date type, extracting only the date part (year, month, day) as required for partitioning. This matches the requirement to convert the timestamp string to a date for partitioning, not a full timestamp.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • df.withColumn('date', col('timestamp').cast('date'))

    Why it's wrong here

    May not parse correctly without format.

  • df.withColumn('date', to_timestamp('timestamp', 'yyyy-MM-dd HH:mm:ss'))

    Why it's wrong here

    Returns timestamp, not date.

  • df.withColumn('date', to_date('timestamp', 'yyyy-MM-dd HH:mm:ss'))

    Why this is correct

    Correctly converts string to date with format.

  • df.withColumn('date', to_date('timestamp'))

    Why it's wrong here

    Missing format; may produce null.

Go deeper

Related to this question

About these practice questions

Courseiva writes every DP-203 question from scratch — 760 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This DP-203 practice question is part of Courseiva's free Microsoft certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DP-203 exam.