Fabric Analytics Engineer (DP-600) · Free practice question 5 of 12
Converting strings to dates in PySpark
A PySpark DataFrame df has a string column OrderDateText with values such as 2026-03-14. You need a new column OrderDate of the date type for loading into a Delta table. Which statement should you use?
- A.df = df.withColumnRenamed("OrderDateText", "OrderDate")
- B.df = df.withColumn("OrderDate", to_date(col("OrderDateText"), "yyyy-MM-dd"))
- C.df = df.withColumn("OrderDate", col("OrderDateText").cast("string"))
- D.df = df.select("OrderDateText").alias("OrderDate")
Show answer and explanation
Correct answer: B. df = df.withColumn("OrderDate", to_date(col("OrderDateText"), "yyyy-MM-dd"))
Why: withColumn adds a column, and to_date parses the string with the yyyy-MM-dd pattern into a date value. Renaming the column keeps it as a string, and casting to string doesn't change the type. Selecting with an alias also leaves the data as text and drops the other columns.
More free Fabric Analytics Engineer (DP-600) questions
- Reviewing access in the catalog Secure tab
- Shortcut caching for cross-cloud egress
- Open mirroring landing zone for custom sources
- Distinct and conditional counts in KQL
- Warehouse replacements for unsupported types
- Top item value with the INDEX function
- Calculated columns in Direct Lake models
- Diagnosing fallback with TABLETRAITS
- Default lakehouse rules for notebooks
- Opening PBIP projects without a pbip file
- Detect data changes in incremental refresh