Parse Day-First Slash Dates (DD/MM/YYYY) in pandas

Quick Overview

Write a pandas function that parses day-first slash dates like 15/03/2021 into a datetime Series while keeping its index and name. Tests explicit format strings in pd.to_datetime, why DD/MM dates are ambiguous, and how strict parsing handles malformed values.

Parse Day-First Slash Dates (DD/MM/YYYY) in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def parse_dmy(s: pd.Series) -> pd.Series`. The input holds day-first slash date strings such as `'15/03/2021'`. Parse them so `'15/03/2021'` becomes March 15, 2021, not the US month-first reading. The function must: - Call `pd.to_datetime(s, format='%d/%m/%Y')`. - Return a Series with dtype `datetime64[ns]`. - Preserve the original index and name. Example: ```python s = pd.Series(['15/03/2021', '01/12/2020', '31/07/1999']) parse_dmy(s) # 0 2021-03-15 # 1 2020-12-01 # 2 1999-07-31 # dtype: datetime64[ns] ``` ```hint Look at the second row Work out what a month-first parser would make of `'01/12/2020'`, and whether anything in the string itself could tell it otherwise. ``` ### What a Strong Answer Covers - Why a day-first string is ambiguous whenever both the day and the month are 12 or less, and how the default month-first preference misreads it. - How each directive in `'%d/%m/%Y'` maps to a field, and why an explicit format makes the parse strict. - That the returned Series keeps the input's index and name and has dtype `datetime64[ns]`. - What happens to malformed strings, impossible dates such as `'31/02/2021'`, and missing values. ### Follow-up Questions - Why is `dayfirst=True` without a `format` a weaker fix than an explicit format string? - How would you parse the column if a few rows were malformed and you wanted to keep the rest? - What would change if the strings also carried a time, such as `'15/03/2021 14:30'`?

Overview: Write a pandas function that parses day-first slash dates like 15/03/2021 into a datetime Series while keeping its index and name. Tests explicit format strings in pd.to_datetime, why DD/MM dates are ambiguous, and how strict parsing handles malformed values.

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

Implement def parse_dmy(s: pd.Series) -> pd.Series. The input holds day-first slash date strings such as '15/03/2021'. Parse them so '15/03/2021' becomes March 15, 2021, not the US month-first reading.

The function must:

  • Call pd.to_datetime(s, format='%d/%m/%Y') .
  • Return a Series with dtype datetime64[ns] .
  • Preserve the original index and name.

Example:

s = pd.Series(['15/03/2021', '01/12/2020', '31/07/1999'])
parse_dmy(s)
# 0   2021-03-15
# 1   2020-12-01
# 2   1999-07-31
# dtype: datetime64[ns]

What a Strong Answer Covers Guidance

  • Why a day-first string is ambiguous whenever both the day and the month are 12 or less, and how the default month-first preference misreads it.
  • How each directive in '%d/%m/%Y' maps to a field, and why an explicit format makes the parse strict.
  • That the returned Series keeps the input's index and name and has dtype datetime64[ns] .
  • What happens to malformed strings, impossible dates such as '31/02/2021' , and missing values.

Follow-up Questions Guidance

  • Why is dayfirst=True without a format a weaker fix than an explicit format string?
  • How would you parse the column if a few rows were malformed and you wanted to keep the rest?
  • What would change if the strings also carried a time, such as '15/03/2021 14:30' ?