Factorize a pandas label Series and reconstruct the labels from the codes

Quick Overview

Write a pandas function that encodes an object-dtype label Series with pd.factorize and rebuilds the labels from the integer codes, keeping the index. It tests first-appearance ordering, handling the -1 missing-value code, dtypes and vectorized NumPy indexing.

Factorize a pandas label Series and reconstruct the labels from the codes

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `factorize_labels(s: pd.Series) -> pd.DataFrame`. The input is an object-dtype Series of labels. Encode it with `codes, uniques = pd.factorize(s)` using the default `sort=False`, so codes follow first-appearance order and a missing value (`None` or `NaN`) gets the code `-1`. Then reconstruct the labels from the codes. The returned DataFrame must have: - A column `code` of dtype `int64` holding the codes from `pd.factorize`. - A column `label` of dtype `object`, where each non-negative code maps back to its label through `uniques` and `-1` maps to `NaN`. - The same index as the input Series. Example: ```python s = pd.Series(['b', 'a', 'b', None, 'a'], index=[10, 11, 12, 13, 14]) factorize_labels(s) # code label # 10 0 b # 11 1 a # 12 0 b # 13 -1 NaN # 14 1 a ``` ```hint Watch the sentinel Before you look up `uniques[code]` for every row, think about what NumPy indexing does with a negative index. ``` ### What a Strong Answer Covers - Calls `pd.factorize` with the default ordering and explains why the codes follow first appearance. - Handles the `-1` code explicitly so a missing value never picks up the last unique label. - Returns `code` as `int64` and `label` as `object`, with the input index carried over unchanged. - Does the mapping with vectorized NumPy indexing, without a per-row Python loop. - Covers edge cases such as an empty Series, an all-missing Series and a non-default index. ### Follow-up Questions - How would `sort=True` change the codes, and would the reconstruction change? - How could you reconstruct the labels in a single indexing step with no mask, or with `pd.Categorical.from_codes`? - New data arrives with labels that were never seen when `uniques` was built. How would you encode it with the existing mapping?

Overview: Write a pandas function that encodes an object-dtype label Series with pd.factorize and rebuilds the labels from the integer codes, keeping the index. It tests first-appearance ordering, handling the -1 missing-value code, dtypes and vectorized NumPy indexing.

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

Implement factorize_labels(s: pd.Series) -> pd.DataFrame. The input is an object-dtype Series of labels. Encode it with codes, uniques = pd.factorize(s) using the default sort=False, so codes follow first-appearance order and a missing value (None or NaN) gets the code -1. Then reconstruct the labels from the codes.

The returned DataFrame must have:

  • A column code of dtype int64 holding the codes from pd.factorize .
  • A column label of dtype object , where each non-negative code maps back to its label through uniques and -1 maps to NaN .
  • The same index as the input Series.

Example:

s = pd.Series(['b', 'a', 'b', None, 'a'], index=[10, 11, 12, 13, 14])
factorize_labels(s)
#     code label
# 10     0     b
# 11     1     a
# 12     0     b
# 13    -1   NaN
# 14     1     a

What a Strong Answer Covers Guidance

  • Calls pd.factorize with the default ordering and explains why the codes follow first appearance.
  • Handles the -1 code explicitly so a missing value never picks up the last unique label.
  • Returns code as int64 and label as object , with the input index carried over unchanged.
  • Does the mapping with vectorized NumPy indexing, without a per-row Python loop.
  • Covers edge cases such as an empty Series, an all-missing Series and a non-default index.

Follow-up Questions Guidance

  • How would sort=True change the codes, and would the reconstruction change?
  • How could you reconstruct the labels in a single indexing step with no mask, or with pd.Categorical.from_codes ?
  • New data arrives with labels that were never seen when uniques was built. How would you encode it with the existing mapping?