Convert T-shirt sizes to an ordered categorical in pandas

Quick Overview

Convert a pandas Series of T-shirt sizes into an ordered categorical so that comparisons and sorting follow S, M, L, XL. It tests how pandas infers category order, the comparison rules for ordered categoricals, and what happens to values outside the allowed set.

Convert T-shirt sizes to an ordered categorical in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `to_ordered_size(s: pd.Series) -> pd.Series`. The input is an object-dtype Series of T-shirt sizes drawn from `'S'`, `'M'`, `'L'` and `'XL'`, with repeats. Convert it to an ordered categorical in which sizes compare by their real order, `'S' < 'M' < 'L' < 'XL'`. Requirements: - Return a Series with dtype `category` and `ordered=True`. - The categories are exactly `['S', 'M', 'L', 'XL']`, in that order. - Keep the original index and name. - Comparisons such as `out < 'L'` follow the size order. Example: ```python s = pd.Series(['M', 'S', 'XL', 'L']) out = to_ordered_size(s) out.dtype # CategoricalDtype(categories=['S', 'M', 'L', 'XL'], ordered=True) (out < 'L') # [True, True, False, False] ``` ```hint Where the order comes from Check which category order pandas picks when it infers the categories from the data, and compare it with the size order. ``` ### What a Strong Answer Covers - Where the category order comes from, and why the order pandas infers from the data is wrong for sizes. - How comparisons and sorting behave on an ordered categorical, including a comparison with a value that is not a category. - What happens to values outside the four sizes, and how to detect them. - Keeping the index and name and returning a new Series. ### Follow-up Questions - What happens if the Series contains a value outside the four categories? - How would you add a new size like `'XXL'` at the top of the order later? What about `'XS'` at the bottom? - Two datasets were each converted with their own list of sizes. What goes wrong when you compare or concatenate them? - How would you turn this column into a feature for a model?

Overview: Convert a pandas Series of T-shirt sizes into an ordered categorical so that comparisons and sorting follow S, M, L, XL. It tests how pandas infers category order, the comparison rules for ordered categoricals, and what happens to values outside the allowed set.

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

Implement to_ordered_size(s: pd.Series) -> pd.Series. The input is an object-dtype Series of T-shirt sizes drawn from 'S', 'M', 'L' and 'XL', with repeats. Convert it to an ordered categorical in which sizes compare by their real order, 'S' < 'M' < 'L' < 'XL'.

Requirements:

  • Return a Series with dtype category and ordered=True .
  • The categories are exactly ['S', 'M', 'L', 'XL'] , in that order.
  • Keep the original index and name.
  • Comparisons such as out < 'L' follow the size order.

Example:

s = pd.Series(['M', 'S', 'XL', 'L'])
out = to_ordered_size(s)
out.dtype        # CategoricalDtype(categories=['S', 'M', 'L', 'XL'], ordered=True)
(out < 'L')      # [True, True, False, False]

What a Strong Answer Covers Guidance

  • Where the category order comes from, and why the order pandas infers from the data is wrong for sizes.
  • How comparisons and sorting behave on an ordered categorical, including a comparison with a value that is not a category.
  • What happens to values outside the four sizes, and how to detect them.
  • Keeping the index and name and returning a new Series.

Follow-up Questions Guidance

  • What happens if the Series contains a value outside the four categories?
  • How would you add a new size like 'XXL' at the top of the order later? What about 'XS' at the bottom?
  • Two datasets were each converted with their own list of sizes. What goes wrong when you compare or concatenate them?
  • How would you turn this column into a feature for a model?