Convert a pandas label Series to an ordered categorical
Quick Overview
Convert a pandas Series of text labels such as small, medium and large into an ordered categorical whose comparisons follow a given low-to-high order. Tests pandas categorical dtypes, keeping the index and name through a conversion, and handling labels outside the category list.
Convert a pandas label Series to an ordered categorical
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `ordered_category(s, categories)`. It takes an object-dtype `pd.Series` of labels such as `'small'`, `'medium'` and `'large'`, and a list `categories` that gives those labels from lowest to highest. Return the data as an ordered categorical, so that comparisons such as `<` follow the order in `categories`.
Requirements:
- Preserve the original `index` and `name` of `s`.
- The result must pass `assert_series_equal` against the expected ordered categorical.
Example:
```python
s = pd.Series(['medium', 'small', 'large'])
cats = ['small', 'medium', 'large']
out = ordered_category(s, cats)
# out.dtype -> CategoricalDtype(categories=['small', 'medium', 'large'], ordered=True)
# out < 'large' -> [True, True, False]
```
```hint Default inference
Check which categories, in which order, pandas picks when you call `s.astype('category')` with nothing else, and whether that result is ordered. Then compare it with the dtype the grader expects.
```
### What a Strong Answer Covers
- Fixing both the set of categories and their low-to-high order explicitly, with the ordered flag set.
- Keeping the index and name of `s` in the result.
- What happens to labels that are missing from `categories` and to values that are already missing.
- Why comparisons and sorting follow the category order after the conversion, and which comparisons still raise.
### Follow-up Questions
- How do you handle labels in `s` that are not in `categories`?
- Why use an ordered categorical instead of mapping the labels to integers?
- What happens if you compare the result with a label that is not one of the categories, or with another ordered categorical whose category list is different?
Overview: Convert a pandas Series of text labels such as small, medium and large into an ordered categorical whose comparisons follow a given low-to-high order. Tests pandas categorical dtypes, keeping the index and name through a conversion, and handling labels outside the category list.
Implement ordered_category(s, categories). It takes an object-dtype pd.Series of labels such as 'small', 'medium' and 'large', and a list categories that gives those labels from lowest to highest. Return the data as an ordered categorical, so that comparisons such as < follow the order in categories.
Requirements:
Preserve the original
index
and
name
of
s
.
The result must pass
assert_series_equal
against the expected ordered categorical.
Example:
s = pd.Series(['medium', 'small', 'large'])
cats = ['small', 'medium', 'large']
out = ordered_category(s, cats)
# out.dtype -> CategoricalDtype(categories=['small', 'medium', 'large'], ordered=True)
# out < 'large' -> [True, True, False]
What a Strong Answer Covers Guidance
Fixing both the set of categories and their low-to-high order explicitly, with the ordered flag set.
Keeping the index and name of
s
in the result.
What happens to labels that are missing from
categories
and to values that are already missing.
Why comparisons and sorting follow the category order after the conversion, and which comparisons still raise.
Follow-up Questions Guidance
How do you handle labels in
s
that are not in
categories
?
Why use an ordered categorical instead of mapping the labels to integers?
What happens if you compare the result with a label that is not one of the categories, or with another ordered categorical whose category list is different?