Bin Ages Into Ordered Groups With pandas pd.cut

Quick Overview

Write a pandas function that maps integer ages to ordered age-group labels with pd.cut and sends out-of-range ages to NaN. It tests how right-closed intervals and include_lowest place boundary values, and how pd.cut builds an ordered Categorical.

Bin Ages Into Ordered Groups With pandas pd.cut

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def bin_ages(s: pd.Series) -> pd.Series`. Given a Series of integer ages, return a new Series that maps each age to a group label. Use `pd.cut` with `bins=[0, 18, 35, 65, 120]`, `labels=['minor', 'young_adult', 'adult', 'senior']`, `right=True` and `include_lowest=True`. The result must meet these requirements: - The intervals are `(0, 18]`, `(18, 35]`, `(35, 65]` and `(65, 120]`, and age `0` also falls in `'minor'`. - The result is an ordered Categorical: `minor < young_adult < adult < senior`. - Ages outside `[0, 120]` become `NaN`. For example: ```python bin_ages(pd.Series([0, 18, 19, 70, 130])) # 0 minor # 1 minor # 2 young_adult # 3 senior # 4 NaN # dtype: category ``` ```hint Boundary ages Trace the edge values 0, 18 and 120 through the intervals by hand. Check which keyword argument decides the side an edge value falls on, and what happens to the very first edge. ``` ### Constraints & Assumptions - The input is a pandas Series of integer ages. Some values may be negative or above 120. - Return a new Series and leave the input unchanged. ### What a Strong Answer Covers - A correct `pd.cut` call with the given bins, labels and flags. - Where the edge ages 0, 18, 35, 65 and 120 land, and which flag decides each case. - Why the result is an ordered Categorical and where the category order comes from. - What happens to out-of-range or missing values and to the input's index. ### Follow-up Questions - How would you change the call so that 18 counts as `young_adult`, while 120 still counts as `senior`? - How would you handle ages stored as strings, such as `"42"` or `"unknown"`? - How would you add an `'unknown'` group for out-of-range ages and keep the ordered dtype? - When would you use `pd.qcut` for age groups instead of fixed edges?

Overview: Write a pandas function that maps integer ages to ordered age-group labels with pd.cut and sends out-of-range ages to NaN. It tests how right-closed intervals and include_lowest place boundary values, and how pd.cut builds an ordered Categorical.

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

Implement def bin_ages(s: pd.Series) -> pd.Series. Given a Series of integer ages, return a new Series that maps each age to a group label. Use pd.cut with bins=[0, 18, 35, 65, 120], labels=['minor', 'young_adult', 'adult', 'senior'], right=True and include_lowest=True.

The result must meet these requirements:

  • The intervals are (0, 18] , (18, 35] , (35, 65] and (65, 120] , and age 0 also falls in 'minor' .
  • The result is an ordered Categorical: minor < young_adult < adult < senior .
  • Ages outside [0, 120] become NaN .

For example:

bin_ages(pd.Series([0, 18, 19, 70, 130]))
# 0          minor
# 1          minor
# 2    young_adult
# 3         senior
# 4            NaN
# dtype: category

Constraints & Assumptions

  • The input is a pandas Series of integer ages. Some values may be negative or above 120.
  • Return a new Series and leave the input unchanged.

What a Strong Answer Covers Guidance

  • A correct pd.cut call with the given bins, labels and flags.
  • Where the edge ages 0, 18, 35, 65 and 120 land, and which flag decides each case.
  • Why the result is an ordered Categorical and where the category order comes from.
  • What happens to out-of-range or missing values and to the input's index.

Follow-up Questions Guidance

  • How would you change the call so that 18 counts as young_adult , while 120 still counts as senior ?
  • How would you handle ages stored as strings, such as "42" or "unknown" ?
  • How would you add an 'unknown' group for out-of-range ages and keep the ordered dtype?
  • When would you use pd.qcut for age groups instead of fixed edges?