Normalize Datetimes to Midnight

Quick Overview

Write a pandas function that sets every timestamp in a datetime64 Series to midnight of the same day while keeping the dtype, index and name. It tests vectorized datetime handling, knowing which pandas methods change the dtype, and how timezones affect where a day starts.

Normalize Datetimes to Midnight

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `normalize_midnight(s: pd.Series) -> pd.Series`. The input `s` is a `datetime64[ns]` Series whose values have non-zero time components. Return a Series in which each value is set to midnight (00:00:00) of the same day. Requirements: - Use a vectorized pandas operation, with no Python-level loop or `apply`. - Return dtype `datetime64[ns]`. A Series of Python `date` objects or of strings does not meet this. - Preserve the original index and name. Example: ```python Timestamp('2021-03-15 14:37:20') -> Timestamp('2021-03-15 00:00:00') ``` ```hint Check the dtype Several pandas calls drop the time of day, but they return different dtypes. Check what each one gives back, and check what it does with a time late in the evening. ``` ### Constraints & Assumptions - `s` has dtype `datetime64[ns]` and carries no timezone. The timezone-aware case is a follow-up. - The index can be of any type and need not be a default range, and the Series may have a name. ### What a Strong Answer Covers - A single vectorized call that moves every value to 00:00:00 of its own day - Why the result keeps dtype `datetime64[ns]`, and how that differs from extracting dates or formatting strings - How the index and name carry through to the result - How the method treats times late in the day, where rounding and truncating land on different days - Edge cases such as missing values (`NaT`) and input that is not yet a datetime dtype ### Follow-up Questions - How would you handle a timezone-aware Series, and what changes if you need midnight in UTC instead of local midnight? - What if you only need the date, not a datetime? What do you give up by switching to Python `date` objects? - What happens on a daylight-saving change in a zone whose clocks jump forward at midnight? - How would you snap timestamps to the start of the hour, the week or the month instead?

Overview: Write a pandas function that sets every timestamp in a datetime64 Series to midnight of the same day while keeping the dtype, index and name. It tests vectorized datetime handling, knowing which pandas methods change the dtype, and how timezones affect where a day starts.

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

Implement normalize_midnight(s: pd.Series) -> pd.Series. The input s is a datetime64[ns] Series whose values have non-zero time components. Return a Series in which each value is set to midnight (00:00:00) of the same day.

Requirements:

  • Use a vectorized pandas operation, with no Python-level loop or apply .
  • Return dtype datetime64[ns] . A Series of Python date objects or of strings does not meet this.
  • Preserve the original index and name.

Example:

Timestamp('2021-03-15 14:37:20') -> Timestamp('2021-03-15 00:00:00')

Constraints & Assumptions

  • s has dtype datetime64[ns] and carries no timezone. The timezone-aware case is a follow-up.
  • The index can be of any type and need not be a default range, and the Series may have a name.

What a Strong Answer Covers Guidance

  • A single vectorized call that moves every value to 00:00:00 of its own day
  • Why the result keeps dtype datetime64[ns] , and how that differs from extracting dates or formatting strings
  • How the index and name carry through to the result
  • How the method treats times late in the day, where rounding and truncating land on different days
  • Edge cases such as missing values ( NaT ) and input that is not yet a datetime dtype

Follow-up Questions Guidance

  • How would you handle a timezone-aware Series, and what changes if you need midnight in UTC instead of local midnight?
  • What if you only need the date, not a datetime? What do you give up by switching to Python date objects?
  • What happens on a daylight-saving change in a zone whose clocks jump forward at midnight?
  • How would you snap timestamps to the start of the hour, the week or the month instead?