Normalize Mixed Encodings to Booleans

Quick Overview

Convert an object Series that mixes Y/N, yes/no, true/false and 1/0 encodings into a clean boolean Series in pandas. It tests dictionary lookups on mixed key types, how missing keys change a column's dtype, and why a plain cast to bool gives wrong results.

Normalize Mixed Encodings to Booleans

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `to_bool(s: pd.Series) -> pd.Series`. The input `s` is an `object` dtype Series that mixes string and integer encodings of true and false. Return a new boolean Series that translates every value with the mapping below. Every value in `s` is guaranteed to be a key of the mapping. Requirements: - Use exactly this mapping: `{'Y': True, 'N': False, 'yes': True, 'no': False, 'true': True, 'false': False, 1: True, 0: False}`. - The result has `bool` dtype and contains no `NaN`. - Preserve the original index and name. - Do not mutate the input. - Do the translation with a pandas operation; do not loop over the elements in Python. Example: ```python s = pd.Series(['Y', 'no', 1, 'false', 0, 'yes']) to_bool(s) # 0 True # 1 False # 2 True # 3 False # 4 False # 5 True # dtype: bool ``` ```hint Think about the dtype Consider what a lookup does with a value it cannot find and what that does to the result's dtype. Also check what a plain cast to `bool` does with a non-empty string such as `'no'`. ``` ### Constraints & Assumptions - `s` has `object` dtype and holds Python `str` and `int` values. - Every value is a key of the mapping, so the input has no unknown or missing values. ### What a Strong Answer Covers - A single lookup that translates every encoding with the given mapping - Why the result comes out as `bool` dtype, and what would turn it into `object` - How the index and name carry through, and why the input stays unchanged - Why casting the raw values with `astype(bool)` gives wrong answers - The time and space cost of the approach ### Follow-up Questions - What if an unexpected value such as `'maybe'` appears? - How does your approach compare with `Series.replace` for this task? - How would you accept case and whitespace variants such as `' Yes '` or `'TRUE'` while still matching the integer keys? - The data also starts to contain the strings `'1'` and `'0'` and real Python booleans. Which of them does the mapping already handle, and why?

Overview: Convert an object Series that mixes Y/N, yes/no, true/false and 1/0 encodings into a clean boolean Series in pandas. It tests dictionary lookups on mixed key types, how missing keys change a column's dtype, and why a plain cast to bool gives wrong results.

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

Implement to_bool(s: pd.Series) -> pd.Series. The input s is an object dtype Series that mixes string and integer encodings of true and false. Return a new boolean Series that translates every value with the mapping below. Every value in s is guaranteed to be a key of the mapping.

Requirements:

  • Use exactly this mapping: {'Y': True, 'N': False, 'yes': True, 'no': False, 'true': True, 'false': False, 1: True, 0: False} .
  • The result has bool dtype and contains no NaN .
  • Preserve the original index and name.
  • Do not mutate the input.
  • Do the translation with a pandas operation; do not loop over the elements in Python.

Example:

s = pd.Series(['Y', 'no', 1, 'false', 0, 'yes'])
to_bool(s)
# 0     True
# 1    False
# 2     True
# 3    False
# 4    False
# 5     True
# dtype: bool

Constraints & Assumptions

  • s has object dtype and holds Python str and int values.
  • Every value is a key of the mapping, so the input has no unknown or missing values.

What a Strong Answer Covers Guidance

  • A single lookup that translates every encoding with the given mapping
  • Why the result comes out as bool dtype, and what would turn it into object
  • How the index and name carry through, and why the input stays unchanged
  • Why casting the raw values with astype(bool) gives wrong answers
  • The time and space cost of the approach

Follow-up Questions Guidance

  • What if an unexpected value such as 'maybe' appears?
  • How does your approach compare with Series.replace for this task?
  • How would you accept case and whitespace variants such as ' Yes ' or 'TRUE' while still matching the integer keys?
  • The data also starts to contain the strings '1' and '0' and real Python booleans. Which of them does the mapping already handle, and why?