Map a Yes/No pandas Series to booleans

Quick Overview

Write a pandas function that converts a Series of Yes and No strings into a boolean Series that keeps its index and name. It tests how Series.map handles dtypes, why unmapped values turn the result into object dtype, and which conversions silently give wrong answers.

Map a Yes/No pandas Series to booleans

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `yesno_to_bool(s: pd.Series) -> pd.Series` that converts an object Series of exactly `'Yes'` and `'No'` strings to booleans using `s.map({'Yes': True, 'No': False})`. There are no missing or unknown values. Requirements: - Map `'Yes'` to `True` and `'No'` to `False`. - The result must have NumPy dtype `bool`, not `object`. - Preserve the original `index` and `name`. Example: ```python s = pd.Series(['Yes', 'No', 'Yes'], name='active') yesno_to_bool(s) # 0 True # 1 False # 2 True # Name: active, dtype: bool ``` ```hint Check the dtype Run the map and look at `.dtype` before adding any conversion. Then ask what a conversion you add would do to a value the dictionary does not cover. ``` ### Constraints & Assumptions - Every value is exactly `'Yes'` or `'No'`: no other spellings, no whitespace, no missing values. - The input has dtype `object`. ### What a Strong Answer Covers - Mapping both labels and returning a Series with NumPy `bool` dtype. - Keeping the input's index and name. - Why `map` with a dictionary that covers every value already returns `bool`, and what would make it return `object` instead. - Conversions that look right but give wrong answers, such as calling `astype(bool)` on the strings themselves. ### Follow-up Questions - What if unknown values or NaNs appeared in the Series? - Why would chaining `.astype(bool)` after the map be risky if the input contract were ever broken? - How would you handle inputs such as `'yes'`, `' YES '` or `'Y'` from a messy source?

Overview: Write a pandas function that converts a Series of Yes and No strings into a boolean Series that keeps its index and name. It tests how Series.map handles dtypes, why unmapped values turn the result into object dtype, and which conversions silently give wrong answers.

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

Write yesno_to_bool(s: pd.Series) -> pd.Series that converts an object Series of exactly 'Yes' and 'No' strings to booleans using s.map({'Yes': True, 'No': False}). There are no missing or unknown values.

Requirements:

  • Map 'Yes' to True and 'No' to False .
  • The result must have NumPy dtype bool , not object .
  • Preserve the original index and name .

Example:

s = pd.Series(['Yes', 'No', 'Yes'], name='active')
yesno_to_bool(s)
# 0     True
# 1    False
# 2     True
# Name: active, dtype: bool

Constraints & Assumptions

  • Every value is exactly 'Yes' or 'No' : no other spellings, no whitespace, no missing values.
  • The input has dtype object .

What a Strong Answer Covers Guidance

  • Mapping both labels and returning a Series with NumPy bool dtype.
  • Keeping the input's index and name.
  • Why map with a dictionary that covers every value already returns bool , and what would make it return object instead.
  • Conversions that look right but give wrong answers, such as calling astype(bool) on the strings themselves.

Follow-up Questions Guidance

  • What if unknown values or NaNs appeared in the Series?
  • Why would chaining .astype(bool) after the map be risky if the input contract were ever broken?
  • How would you handle inputs such as 'yes' , ' YES ' or 'Y' from a messy source?