Map Status Labels With Series.map and NaN for Unknown Codes

Quick Overview

Translate single-letter status codes in a pandas Series to full labels with a vectorized Series.map, turning codes missing from the mapping into NaN. It tests how map differs from replace, keeping the index and name, and adding a fallback for unknown codes.

Map Status Labels With Series.map and NaN for Unknown Codes

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `map_status_codes(s: pd.Series) -> pd.Series` that translates status codes to full descriptions via the vectorized `s.map(mapping)` (no loops) with this fixed mapping: ```python mapping = {'A': 'Active', 'I': 'Inactive', 'P': 'Pending'} ``` Requirements: - Labels absent from `mapping` become `NaN` (they are not left unchanged as they would be with `.replace()`). - Return an object-dtype Series preserving `index` and `name` (compared via `assert_series_equal`). Example: ```python s = pd.Series(['A', 'P', 'X', 'I'], name='status') map_status_codes(s) # 0 Active # 1 Pending # 2 NaN # 3 Inactive # Name: status, dtype: object ``` ```hint Try both lookups Run `.map()` and `.replace()` on a small Series that contains a code missing from the dict, and compare what each one does with that code. ``` ### What a Strong Answer Covers - Calling `s.map` with the fixed dictionary and no Python loop or `apply`. - Why an unknown code such as `'X'` ends up as `NaN`, and why `.replace()` would fail the requirement. - Confirming that the result keeps the index and name and has object dtype. - How to tell codes that were unknown apart from values that were already missing in `s`. ### Follow-up Questions - When would `.replace()` be the better choice than `.map()`? - How would you supply a fallback instead of `NaN` for unknown labels? - How would you report which unknown codes appeared, and how often, before they turn into `NaN`?

Overview: Translate single-letter status codes in a pandas Series to full labels with a vectorized Series.map, turning codes missing from the mapping into NaN. It tests how map differs from replace, keeping the index and name, and adding a fallback for unknown codes.

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

Implement map_status_codes(s: pd.Series) -> pd.Series that translates status codes to full descriptions via the vectorized s.map(mapping) (no loops) with this fixed mapping:

mapping = {'A': 'Active', 'I': 'Inactive', 'P': 'Pending'}

Requirements:

  • Labels absent from mapping become NaN (they are not left unchanged as they would be with .replace() ).
  • Return an object-dtype Series preserving index and name (compared via assert_series_equal ).

Example:

s = pd.Series(['A', 'P', 'X', 'I'], name='status')
map_status_codes(s)
# 0      Active
# 1     Pending
# 2         NaN
# 3    Inactive
# Name: status, dtype: object

What a Strong Answer Covers Guidance

  • Calling s.map with the fixed dictionary and no Python loop or apply .
  • Why an unknown code such as 'X' ends up as NaN , and why .replace() would fail the requirement.
  • Confirming that the result keeps the index and name and has object dtype.
  • How to tell codes that were unknown apart from values that were already missing in s .

Follow-up Questions Guidance

  • When would .replace() be the better choice than .map() ?
  • How would you supply a fallback instead of NaN for unknown labels?
  • How would you report which unknown codes appeared, and how often, before they turn into NaN ?