Compute Whole-Year Age From Two Datetime Columns in pandas

Quick Overview

Write a vectorized pandas function that returns each row's whole-year age from a date of birth column and an as-of datetime column. It tests calendar date comparison with the .dt accessor, the birthday boundary, and edge cases such as Feb 29 births and missing dates.

Compute Whole-Year Age From Two Datetime Columns in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `age_in_years(df: pd.DataFrame) -> pd.Series`. The frame has two `datetime64[ns]` columns, `dob` (date of birth) and `asof` (the date on which age is measured). Return each row's age in whole years as of `asof`. The age follows the calendar rule: the difference in years, minus 1 when the birthday has not yet come around in the `asof` year. As pseudocode: ```text age = asof.year - dob.year - ((asof.month, asof.day) < (dob.month, dob.day)) ``` Requirements: - Compute it vectorized through the `.dt` accessor, with no row-wise loops. - The age goes up on or after the birthday within the `asof` year, so an `asof` that falls on the birthday already counts the new year. - Return an integer Series named `age` that keeps the original index of `df`. Example: ```text dob = 2000-06-15, asof = 2021-06-14 -> 20 dob = 2000-06-15, asof = 2021-06-15 -> 21 ``` ```hint Comparing month and day A tuple of two Series does not compare elementwise in pandas. Think about how to turn each (month, day) pair into a single number per row that sorts in the same order. ``` ### Constraints & Assumptions - Both columns are timezone-naive `datetime64[ns]`. - The base task assumes no missing dates; one of the follow-ups asks about `NaT`. ### What a Strong Answer Covers - A vectorized way to compare (month, day) pairs across two Series, and why a literal tuple comparison on Series does not work - The boundary on the birthday itself, checked against both examples - Why dividing a day count by 365 or 365.25 is the wrong tool for whole-year age - The dtype, name and index of the returned Series ### Follow-up Questions - How would you handle Feb 29 birthdays when the `asof` year is not a leap year? - What happens if either column contains `NaT`, and what should the function return for those rows? - Why not compute `(asof - dob).dt.days // 365.25`? Give a date pair where it returns the wrong age.

Overview: Write a vectorized pandas function that returns each row's whole-year age from a date of birth column and an as-of datetime column. It tests calendar date comparison with the .dt accessor, the birthday boundary, and edge cases such as Feb 29 births and missing dates.

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

Implement age_in_years(df: pd.DataFrame) -> pd.Series. The frame has two datetime64[ns] columns, dob (date of birth) and asof (the date on which age is measured). Return each row's age in whole years as of asof.

The age follows the calendar rule: the difference in years, minus 1 when the birthday has not yet come around in the asof year. As pseudocode:

age = asof.year - dob.year - ((asof.month, asof.day) < (dob.month, dob.day))

Requirements:

  • Compute it vectorized through the .dt accessor, with no row-wise loops.
  • The age goes up on or after the birthday within the asof year, so an asof that falls on the birthday already counts the new year.
  • Return an integer Series named age that keeps the original index of df .

Example:

dob = 2000-06-15, asof = 2021-06-14  ->  20
dob = 2000-06-15, asof = 2021-06-15  ->  21

Constraints & Assumptions

  • Both columns are timezone-naive datetime64[ns] .
  • The base task assumes no missing dates; one of the follow-ups asks about NaT .

What a Strong Answer Covers Guidance

  • A vectorized way to compare (month, day) pairs across two Series, and why a literal tuple comparison on Series does not work
  • The boundary on the birthday itself, checked against both examples
  • Why dividing a day count by 365 or 365.25 is the wrong tool for whole-year age
  • The dtype, name and index of the returned Series

Follow-up Questions Guidance

  • How would you handle Feb 29 birthdays when the asof year is not a leap year?
  • What happens if either column contains NaT , and what should the function return for those rows?
  • Why not compute (asof - dob).dt.days // 365.25 ? Give a date pair where it returns the wrong age.