Bin Ages Into Right-Closed Labeled Bins With pandas pd.cut

Quick Overview

Implement a pandas function that groups integer ages into child, young, adult and senior bins with pd.cut and right-closed intervals. It tests where edge values such as 18 land, which ages fall outside every bin, and how the result keeps the index, the name and an ordered categorical dtype.

Bin Ages Into Right-Closed Labeled Bins With pandas pd.cut

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `bin_ages(s: pd.Series) -> pd.Series` that groups integer ages into four labeled categories using `pd.cut` and returns an ordered categorical Series. The function must meet these requirements: - Use `pd.cut(s, bins=[0, 18, 35, 65, 120], labels=['child', 'young', 'adult', 'senior'], right=True)`. - Return an ordered `CategoricalDtype`: `child < young < adult < senior`. - Preserve the input's `index` and `name`. For example: ```python s = pd.Series([5, 18, 30, 70], name='age') bin_ages(s) # 0 child # 1 child # 2 young # 3 senior # Name: age, dtype: category # Categories (4, object): ['child' < 'young' < 'adult' < 'senior'] ``` Age 18 lands in `child` because `right=True` makes the first interval `(0, 18]`. ```hint Interval edges Write out the four intervals that these edges and `right=True` produce, and mark which side of each one is closed. Then check which ages fit in none of them. ``` ### Constraints & Assumptions - The input is a pandas Series of integer ages. It may have a non-default index and a name. ### What a Strong Answer Covers - A `pd.cut` call with the given edges, labels and `right=True`, returned without extra conversion. - Which interval each edge value (0, 18, 35, 65, 120) falls in under right-closed bins. - Why the returned dtype is ordered and how the label list sets the order. - That the index and name carry over, and what happens to values outside every interval. ### Follow-up Questions - How would left-closed bins (`right=False`) change where 18 lands? - What happens to ages outside the outer edges, and how would you handle them? - How would you count people per group so that empty groups still appear, in age order?

Overview: Implement a pandas function that groups integer ages into child, young, adult and senior bins with pd.cut and right-closed intervals. It tests where edge values such as 18 land, which ages fall outside every bin, and how the result keeps the index, the name and an ordered categorical dtype.

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

Implement bin_ages(s: pd.Series) -> pd.Series that groups integer ages into four labeled categories using pd.cut and returns an ordered categorical Series.

The function must meet these requirements:

  • Use pd.cut(s, bins=[0, 18, 35, 65, 120], labels=['child', 'young', 'adult', 'senior'], right=True) .
  • Return an ordered CategoricalDtype : child < young < adult < senior .
  • Preserve the input's index and name .

For example:

s = pd.Series([5, 18, 30, 70], name='age')
bin_ages(s)
# 0     child
# 1     child
# 2     young
# 3    senior
# Name: age, dtype: category
# Categories (4, object): ['child' < 'young' < 'adult' < 'senior']

Age 18 lands in child because right=True makes the first interval (0, 18].

Constraints & Assumptions

  • The input is a pandas Series of integer ages. It may have a non-default index and a name.

What a Strong Answer Covers Guidance

  • A pd.cut call with the given edges, labels and right=True , returned without extra conversion.
  • Which interval each edge value (0, 18, 35, 65, 120) falls in under right-closed bins.
  • Why the returned dtype is ordered and how the label list sets the order.
  • That the index and name carry over, and what happens to values outside every interval.

Follow-up Questions Guidance

  • How would left-closed bins ( right=False ) change where 18 lands?
  • What happens to ages outside the outer edges, and how would you handle them?
  • How would you count people per group so that empty groups still appear, in age order?