Extract Calendar Year From Timestamps in a pandas Series

Quick Overview

Write a pandas function that takes a datetime Series and returns each row's calendar year as int32 while keeping the original index and name. It tests the .dt accessor, explicit dtype casting that holds across pandas versions, and awareness of missing or timezone-aware timestamps.

Extract Calendar Year From Timestamps in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def extract_year(s: pd.Series) -> pd.Series`. The input `s` has dtype `datetime64[ns]`. Return each date's calendar year using `s.dt.year`, so `Timestamp('2021-03-15')` becomes `2021`. The result must meet two requirements: - Its dtype must be `int32`. Depending on the pandas version, `s.dt.year` comes back as `int64` or `int32`, so cast it explicitly with `.astype('int32')`. - It keeps the `index` and `name` of `s`. ```hint Keep the labels Rebuilding a Series from a NumPy array drops its index and name. Stay with Series operations that carry both along. ``` ### Constraints & Assumptions - `s` has dtype `datetime64[ns]`. - `s` contains no missing (`NaT`) values. - The output has one value per row of `s`, in the same order. ### What a Strong Answer Covers - Reading the year through the `.dt` accessor in one vectorized call, with no Python loop over rows - An explicit cast to `int32`, and why the default dtype cannot be trusted across pandas versions - Confirmation that the index and name of `s` survive both the accessor and the cast - What would break if the input held `NaT` values or was not a datetime dtype ### Follow-up Questions - If `NaT` values were allowed, what would `.astype('int32')` do, and how would you return a year column that keeps the missing rows? - The timestamps are timezone-aware and stored in UTC, but reports follow the New York calendar year. What changes? - How would you return a fiscal year that starts on July 1 and is named after the calendar year it ends in?

Overview: Write a pandas function that takes a datetime Series and returns each row's calendar year as int32 while keeping the original index and name. It tests the .dt accessor, explicit dtype casting that holds across pandas versions, and awareness of missing or timezone-aware timestamps.

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

Implement def extract_year(s: pd.Series) -> pd.Series. The input s has dtype datetime64[ns]. Return each date's calendar year using s.dt.year, so Timestamp('2021-03-15') becomes 2021.

The result must meet two requirements:

  • Its dtype must be int32 . Depending on the pandas version, s.dt.year comes back as int64 or int32 , so cast it explicitly with .astype('int32') .
  • It keeps the index and name of s .

Constraints & Assumptions

  • s has dtype datetime64[ns] .
  • s contains no missing ( NaT ) values.
  • The output has one value per row of s , in the same order.

What a Strong Answer Covers Guidance

  • Reading the year through the .dt accessor in one vectorized call, with no Python loop over rows
  • An explicit cast to int32 , and why the default dtype cannot be trusted across pandas versions
  • Confirmation that the index and name of s survive both the accessor and the cast
  • What would break if the input held NaT values or was not a datetime dtype

Follow-up Questions Guidance

  • If NaT values were allowed, what would .astype('int32') do, and how would you return a year column that keeps the missing rows?
  • The timestamps are timezone-aware and stored in UTC, but reports follow the New York calendar year. What changes?
  • How would you return a fiscal year that starts on July 1 and is named after the calendar year it ends in?