Parse ISO Date Strings Into a Datetime Column in Pandas Without Mutating the Input

Quick Overview

Write a pandas function that adds a parsed datetime column to a DataFrame of ISO date strings and leaves the input untouched. It tests pd.to_datetime, copy semantics, datetime dtypes and time units, and how to deal with malformed date strings.

Parse ISO Date Strings Into a Datetime Column in Pandas Without Mutating the Input

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `parse_dates(df: pd.DataFrame) -> pd.DataFrame`. The input has a string column `'ts'` of ISO dates (for example `'2023-01-05'`). Return a copy with an added column `'ts_parsed'` equal to `pd.to_datetime` applied to `'ts'`. Requirements: - `'ts'` stays unchanged. - `'ts_parsed'` dtype is `datetime64[ns]`. - Do not mutate the input: operate on and return a copy. Example: ```python df = pd.DataFrame({'ts': ['2023-01-05', '2023-02-10']}) out = parse_dates(df) # out['ts'] -> ['2023-01-05', '2023-02-10'] (unchanged, str) # out['ts_parsed'] -> [2023-01-05, 2023-02-10] (datetime64[ns]) ``` ```hint Check what you return After calling your function, print `out.dtypes` and also look at `df.columns`. Both should match the requirements exactly. ``` ### What a Strong Answer Covers - Working on a copy, and why adding a column to the argument itself would change the caller's DataFrame. - Parsing `'ts'` with `pd.to_datetime` into a new column while `'ts'` keeps its strings. - Making sure the dtype is exactly `datetime64[ns]`, including the time unit. - What happens with malformed or non-ISO strings. ### Follow-up Questions - How would you handle malformed date strings without raising? - How would you make the parsing stricter and faster when every value has the same ISO format? - What changes if some strings carry time zone offsets?

Overview: Write a pandas function that adds a parsed datetime column to a DataFrame of ISO date strings and leaves the input untouched. It tests pd.to_datetime, copy semantics, datetime dtypes and time units, and how to deal with malformed date strings.

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

Implement parse_dates(df: pd.DataFrame) -> pd.DataFrame. The input has a string column 'ts' of ISO dates (for example '2023-01-05'). Return a copy with an added column 'ts_parsed' equal to pd.to_datetime applied to 'ts'.

Requirements:

  • 'ts' stays unchanged.
  • 'ts_parsed' dtype is datetime64[ns] .
  • Do not mutate the input: operate on and return a copy.

Example:

df = pd.DataFrame({'ts': ['2023-01-05', '2023-02-10']})
out = parse_dates(df)
# out['ts']         -> ['2023-01-05', '2023-02-10']  (unchanged, str)
# out['ts_parsed']  -> [2023-01-05, 2023-02-10]      (datetime64[ns])

What a Strong Answer Covers Guidance

  • Working on a copy, and why adding a column to the argument itself would change the caller's DataFrame.
  • Parsing 'ts' with pd.to_datetime into a new column while 'ts' keeps its strings.
  • Making sure the dtype is exactly datetime64[ns] , including the time unit.
  • What happens with malformed or non-ISO strings.

Follow-up Questions Guidance

  • How would you handle malformed date strings without raising?
  • How would you make the parsing stricter and faster when every value has the same ISO format?
  • What changes if some strings carry time zone offsets?