Floor Timestamps to the Hour

Quick Overview

Write a pandas function that truncates every timestamp in a datetime Series to the start of its hour while keeping the index, name and dtype. It tests vectorized datetime handling, the difference between flooring and rounding, and how timezone-aware data behaves around DST changes.

Floor Timestamps to the Hour

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def floor_hour(s: pd.Series) -> pd.Series`. Given a `datetime64[ns]` Series, truncate each timestamp down to the start of its hour using `s.dt.floor('h')` (not `round`). The function must: - Preserve the original index and `name`. - Return dtype `datetime64[ns]`. Example: ```python s = pd.Series(pd.to_datetime([ '2021-03-15 14:37:52', '2021-03-15 14:59:59', '2021-03-15 15:00:00', ])) floor_hour(s) # 0 2021-03-15 14:00:00 # 1 2021-03-15 14:00:00 # 2 2021-03-15 15:00:00 # dtype: datetime64[ns] ``` ```hint Floor versus round Work out where 14:59:59 lands under `floor` and under `round`. Only one of them always moves a timestamp back to the start of its own hour, and a value already on the hour must stay where it is. ``` ### What a Strong Answer Covers - A vectorized call through the `.dt` accessor, with no Python loop over rows. - What `round` or `ceil` would return for the example values, and why only `floor` gives the start of the hour. - How the result keeps the index, the `name` and the `datetime64[ns]` dtype without rebuilding the Series. - Edge cases: values already on an hour boundary, `NaT`, and input that is not yet a datetime dtype. ### Follow-up Questions - How would you floor to a different frequency such as 15 minutes? - What breaks if the Series is timezone-aware, and how do you handle DST? - How would you count events per hour once the timestamps are floored?

Overview: Write a pandas function that truncates every timestamp in a datetime Series to the start of its hour while keeping the index, name and dtype. It tests vectorized datetime handling, the difference between flooring and rounding, and how timezone-aware data behaves around DST changes.

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

Implement def floor_hour(s: pd.Series) -> pd.Series. Given a datetime64[ns] Series, truncate each timestamp down to the start of its hour using s.dt.floor('h') (not round).

The function must:

  • Preserve the original index and name .
  • Return dtype datetime64[ns] .

Example:

s = pd.Series(pd.to_datetime([
    '2021-03-15 14:37:52',
    '2021-03-15 14:59:59',
    '2021-03-15 15:00:00',
]))
floor_hour(s)
# 0   2021-03-15 14:00:00
# 1   2021-03-15 14:00:00
# 2   2021-03-15 15:00:00
# dtype: datetime64[ns]

What a Strong Answer Covers Guidance

  • A vectorized call through the .dt accessor, with no Python loop over rows.
  • What round or ceil would return for the example values, and why only floor gives the start of the hour.
  • How the result keeps the index, the name and the datetime64[ns] dtype without rebuilding the Series.
  • Edge cases: values already on an hour boundary, NaT , and input that is not yet a datetime dtype.

Follow-up Questions Guidance

  • How would you floor to a different frequency such as 15 minutes?
  • What breaks if the Series is timezone-aware, and how do you handle DST?
  • How would you count events per hour once the timestamps are floored?