Coerce Mixed Strings to Dates in Pandas

Quick Overview

Write a pandas function that converts a Series of date strings to timestamps and turns unparseable entries, such as empty strings or impossible calendar dates, into missing values instead of raising. It tests pandas date parsing options, dtype guarantees and keeping the index and name of the input intact.

Coerce Mixed Strings to Dates in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def coerce_dates(s: pd.Series) -> pd.Series`. The input mixes valid ISO dates (`'2022-01-05'`) with unparseable values (`'not a date'`, `'2022-13-40'`, `''`). Valid values become Timestamps and unparseable values become `NaT` instead of raising. Requirements: - Use `pd.to_datetime(s, errors='coerce')`. - The result dtype must be `datetime64[ns]`. - Preserve the index and `name` of `s`. Example: ```python s = pd.Series(['2022-01-05', 'not a date', '2022-13-40', '']) coerce_dates(s) # 0 2022-01-05 # 1 NaT # 2 NaT # 3 NaT # dtype: datetime64[ns] ``` ```hint Check the return type Compare what `pd.to_datetime` returns for a Series with what it returns for a list or array, and confirm the dtype it reports, including when every value fails to parse. ``` ### What a Strong Answer Covers - What `errors='coerce'` does with a bad value compared with the default `errors='raise'`. - Why the output keeps the index and `name` of the input. - How the answer guarantees a `datetime64[ns]` result, including a Series where nothing parses. - Which inputs end up as `NaT`: empty strings, impossible calendar dates, existing nulls and dates outside the representable range. - The cost of the vectorized call compared with a row-by-row Python loop. ### Follow-up Questions - How would you list the raw values that failed to parse so the data owner can fix them at the source? - The column also holds valid dates such as `'01/05/2022'` and `'5 Jan 2022'`. How do you parse them without silently turning them into `NaT`? - Some strings carry timezone offsets. What happens to the dtype, and how would you produce a naive UTC column? - How would you speed up the conversion on a column with tens of millions of rows?

Overview: Write a pandas function that converts a Series of date strings to timestamps and turns unparseable entries, such as empty strings or impossible calendar dates, into missing values instead of raising. It tests pandas date parsing options, dtype guarantees and keeping the index and name of the input intact.

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

Implement def coerce_dates(s: pd.Series) -> pd.Series. The input mixes valid ISO dates ('2022-01-05') with unparseable values ('not a date', '2022-13-40', ''). Valid values become Timestamps and unparseable values become NaT instead of raising.

Requirements:

  • Use pd.to_datetime(s, errors='coerce') .
  • The result dtype must be datetime64[ns] .
  • Preserve the index and name of s .

Example:

s = pd.Series(['2022-01-05', 'not a date', '2022-13-40', ''])
coerce_dates(s)
# 0   2022-01-05
# 1          NaT
# 2          NaT
# 3          NaT
# dtype: datetime64[ns]

What a Strong Answer Covers Guidance

  • What errors='coerce' does with a bad value compared with the default errors='raise' .
  • Why the output keeps the index and name of the input.
  • How the answer guarantees a datetime64[ns] result, including a Series where nothing parses.
  • Which inputs end up as NaT : empty strings, impossible calendar dates, existing nulls and dates outside the representable range.
  • The cost of the vectorized call compared with a row-by-row Python loop.

Follow-up Questions Guidance

  • How would you list the raw values that failed to parse so the data owner can fix them at the source?
  • The column also holds valid dates such as '01/05/2022' and '5 Jan 2022' . How do you parse them without silently turning them into NaT ?
  • Some strings carry timezone offsets. What happens to the dtype, and how would you produce a naive UTC column?
  • How would you speed up the conversion on a column with tens of millions of rows?