Normalize Timestamps to ISO Date Strings

Quick Overview

Convert a pandas datetime64 Series into ISO YYYY-MM-DD date strings while keeping the index and name. It tests explicit date formatting, how missing timestamps come through the conversion, and why generic string casts produce inconsistent output.

Normalize Timestamps to ISO Date Strings

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `to_iso_strings(s: pd.Series) -> pd.Series`. The input `s` is a `datetime64[ns]` Series. Convert each value to an ISO `YYYY-MM-DD` date string, dropping the time component. Requirements: - The returned dtype must be `object` holding strings, not a datetime dtype. - Preserve the original index and name. - Use a vectorized pandas operation; do not loop over the values in Python. Example: ```python s = pd.Series(pd.to_datetime(['2021-03-05 14:20', '2020-12-31 00:00'])) to_iso_strings(s) # 0 2021-03-05 # 1 2020-12-31 # dtype: object ``` ```hint Control the format Pick a method that lets you state the output format explicitly. Some string conversions choose a format based on the data, so the same code can produce differently shaped strings for different inputs. ``` ### Constraints & Assumptions - `s` has dtype `datetime64[ns]` and no timezone. - Times of day can be anything, including exactly midnight. ### What a Strong Answer Covers - A vectorized conversion that formats each value as `YYYY-MM-DD` with an explicit format - Why the time of day disappears and the result holds strings in an `object` Series - How the index and name carry through to the result - What happens to `NaT`, and whether that meets the caller's needs - Why a generic string cast of the column is less reliable ### Follow-up Questions - How would you handle `NaT` values in the Series? - How does the answer change for a timezone-aware Series, or when the date must be the UTC date? - When would you keep the column as datetimes and format it only at export time? - How would you parse the strings back into datetimes, and what does the round trip lose?

Overview: Convert a pandas datetime64 Series into ISO YYYY-MM-DD date strings while keeping the index and name. It tests explicit date formatting, how missing timestamps come through the conversion, and why generic string casts produce inconsistent output.

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

Write to_iso_strings(s: pd.Series) -> pd.Series. The input s is a datetime64[ns] Series. Convert each value to an ISO YYYY-MM-DD date string, dropping the time component.

Requirements:

  • The returned dtype must be object holding strings, not a datetime dtype.
  • Preserve the original index and name.
  • Use a vectorized pandas operation; do not loop over the values in Python.

Example:

s = pd.Series(pd.to_datetime(['2021-03-05 14:20', '2020-12-31 00:00']))
to_iso_strings(s)
# 0    2021-03-05
# 1    2020-12-31
# dtype: object

Constraints & Assumptions

  • s has dtype datetime64[ns] and no timezone.
  • Times of day can be anything, including exactly midnight.

What a Strong Answer Covers Guidance

  • A vectorized conversion that formats each value as YYYY-MM-DD with an explicit format
  • Why the time of day disappears and the result holds strings in an object Series
  • How the index and name carry through to the result
  • What happens to NaT , and whether that meets the caller's needs
  • Why a generic string cast of the column is less reliable

Follow-up Questions Guidance

  • How would you handle NaT values in the Series?
  • How does the answer change for a timezone-aware Series, or when the date must be the UTC date?
  • When would you keep the column as datetimes and format it only at export time?
  • How would you parse the strings back into datetimes, and what does the round trip lose?