Practice / parsing dates

Orders in March

easy

The Daily Grind wants every order placed in March 2026. The till writes the day as text, most of the time as yyyy-MM-dd; a couple of rows were typed by hand the other way round, and one is blank.

You have the order_log DataFrame:

Your task: return order_id, drink and ordered_on as a date for every order placed in March 2026. Rows whose text is not a yyyy-MM-dd date cannot be parsed and must be left out, and they must not stop the run.

Assign your answer DataFrame to result. Row order doesn't matter.

order_loginput DataFrame

Schema
columntype
order_idlong
drinkstring
ordered_onstring
Sample rows (first few)
order_iddrinkordered_on
4001oat latte2026-02-27
4002flat white2026-03-01
4003oat latte2026-03-02
4004mocha2026-03-15
4005espresso2026-03-31
4006flat white2026-04-01
4007cold brew2026-04-18
4008oat latte2026-02-14
4009drip coffee2025-03-09
4010mocha18/04/2026
Expected output shape
order_iddrinkordered_on· 4 rows
›Hint

One bad row fails the whole parse, so parse inside a guard: F.when(F.col("ordered_on").rlike("^[0-9]{4}-[0-9]{2}-[0-9]{2}$"), F.to_date("ordered_on", "yyyy-MM-dd")) runs to_date only on rows that match and leaves the rest null. Then compare F.trunc("ordered_on", "month") with "2026-03-01"; the nulls drop out there. F.month alone would also match March of another year.

›Lesson refresher

This problem builds on A Date Is Not a String (~4 min). Pop it open in a new tab if you want a quick recap.

Loading editor…
Hit Run to execute your code and see the output here.