Understand the idea
The .dt accessor extracts calendar information from a datetime Series. Parse text first; missing dates yield missing calendar values.
A small example
For 2026-08-04: year is 2026, month is 8 and day_name() is "Tuesday".
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d", errors="coerce")
df["year"] = df["date"].dt.year
df["month"] = df["date"].dt.month
df["weekday"] = df["date"].dt.day_name()
dfWhat each part does
df["date"].dt.year- calendar year
.dt.month- month number
.dt.day_name()- weekday name
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| order | drink | size | price | tip | date |
|---|---|---|---|---|---|
| 101 | " latte " | LARGE | 6.20 | 1 | 2026-06-01 |
| 102 | TEA | small | 3.10 | 0.5 | 2026-06-02 |
| 103 | " mocha " | LARGE | oops | None | not a date |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 105 | "tea " | SMALL | 4.20 | 0.6 | 2026-06-05 |
| 106 | ESPRESSO | small | 2.50 | 0.2 | 2026-06-06 |
| 107 | " mocha" | large | 6.80 | 1.5 | 2026-06-07 |
Your task · Follow
- Using df, parse date, then add year, month and weekday columns.
- Retain invalid dates as missing.
- Keep the changes in df and display it.
- Use: to_datetime().
Hint
Parse dates before using .dt; year and month have no parentheses, but day_name does.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d", errors="coerce")
df["year"] = df["date"].dt.year
df["month"] = df["date"].dt.month
df["weekday"] = df["date"].dt.day_name()
df