Parse a Mixed-Format Date Series in pandas

Quick Overview

Write a pandas function that parses a date column mixing ISO, US month-first and month-name strings, with junk values turned into NaT. Tests per-element format inference in pd.to_datetime, the dayfirst and errors options, and the speed and ambiguity costs of parsing each value on its own.

Parse a Mixed-Format Date Series in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `def parse_mixed(s: pd.Series) -> pd.Series`. Each element of `s` may be ISO (`'2021-03-15'`), US-style (`'03/15/2021'`), month-name (`'15 March 2021'`) or junk. Parse with `pd.to_datetime(s, format='mixed', dayfirst=False, errors='coerce')` and return the result. The function must meet these requirements: - Ambiguous numeric dates resolve month-first (`dayfirst=False`). - Unparseable values coerce to `NaT` instead of raising. - The result has dtype `datetime64[ns]` and keeps the original `index` and `name`. Example: ```python s = pd.Series(['2021-03-15', '03/15/2021', '15 March 2021', 'nope']) parse_mixed(s) # 0 2021-03-15 # 1 2021-03-15 # 2 2021-03-15 # 3 NaT # dtype: datetime64[ns] ``` ```hint Think about the default Work out what `pd.to_datetime` would do with this column if `format` were left out, starting from the first value. ``` ```hint Where ambiguity lives Find a string in one of these layouts that two different dates could produce, and decide which argument settles it. ``` ### What a Strong Answer Covers - What `format='mixed'` does, and why one fixed format string cannot cover this column. - How `dayfirst=False` resolves numeric dates where both fields are 12 or less, and which strings it does not affect. - How `errors='coerce'` isolates junk as `NaT` while keeping the valid rows, and how to find the rows that were coerced. - That the result keeps the index and name and has dtype `datetime64[ns]`. - The cost of per-element inference on a large Series. ### Follow-up Questions - How does `format='mixed'` differ from omitting `format` entirely? - Why is per-element inference slow, and how would you speed it up? - What goes wrong if the column mixes US month-first and European day-first slash dates?

Overview: Write a pandas function that parses a date column mixing ISO, US month-first and month-name strings, with junk values turned into NaT. Tests per-element format inference in pd.to_datetime, the dayfirst and errors options, and the speed and ambiguity costs of parsing each value on its own.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement def parse_mixed(s: pd.Series) -> pd.Series. Each element of s may be ISO ('2021-03-15'), US-style ('03/15/2021'), month-name ('15 March 2021') or junk. Parse with pd.to_datetime(s, format='mixed', dayfirst=False, errors='coerce') and return the result.

The function must meet these requirements:

  • Ambiguous numeric dates resolve month-first ( dayfirst=False ).
  • Unparseable values coerce to NaT instead of raising.
  • The result has dtype datetime64[ns] and keeps the original index and name .

Example:

s = pd.Series(['2021-03-15', '03/15/2021', '15 March 2021', 'nope'])
parse_mixed(s)
# 0   2021-03-15
# 1   2021-03-15
# 2   2021-03-15
# 3          NaT
# dtype: datetime64[ns]

What a Strong Answer Covers Guidance

  • What format='mixed' does, and why one fixed format string cannot cover this column.
  • How dayfirst=False resolves numeric dates where both fields are 12 or less, and which strings it does not affect.
  • How errors='coerce' isolates junk as NaT while keeping the valid rows, and how to find the rows that were coerced.
  • That the result keeps the index and name and has dtype datetime64[ns] .
  • The cost of per-element inference on a large Series.

Follow-up Questions Guidance

  • How does format='mixed' differ from omitting format entirely?
  • Why is per-element inference slow, and how would you speed it up?
  • What goes wrong if the column mixes US month-first and European day-first slash dates?