Convert True/False strings to a nullable boolean pandas Series

Quick Overview

Write a pandas function that converts a Series of True and False strings, mixed with blanks, None and junk values, into a nullable boolean Series that keeps the index and name. It tests explicit value mapping, the pandas boolean extension dtype and careful handling of missing data.

Convert True/False strings to a nullable boolean pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def to_nullable_bool(s: pd.Series) -> pd.Series` that converts an object Series of `'True'`/`'False'` strings, possibly mixed with missing markers (empty strings, `None`, other junk), into a nullable boolean Series. Requirements: - Map `'True'` to `True`, `'False'` to `False`, and anything else (missing or unrecognised) to `pd.NA`. - Return dtype `boolean` (the nullable extension type, lowercase), not `bool` or `object`. - Preserve the input `index` and `name`. Example: ```python s = pd.Series(['True', 'False', '', None, 'maybe'], name='flag') to_nullable_bool(s) # 0 True # 1 False # 2 <NA> # 3 <NA> # 4 <NA> # Name: flag, dtype: boolean ``` ```hint Start from what you accept Decide which inputs are valid and let every other value fall through to missing. Then check what each cast you might use does to a `NaN` or a non-empty string. ``` ### What a Strong Answer Covers - An allow-list of the exact tokens `'True'` and `'False'`, so that every other value becomes missing. - The cast to the nullable `boolean` dtype, and why NumPy `bool` cannot represent the missing entries. - Preservation of the index and the name. - Behavior on `None`, `NaN`, empty strings, unknown strings, non-string objects and an empty Series. - A vectorized, linear-time implementation. ### Follow-up Questions - How would you handle case-insensitive or padded inputs like `' true '`? - Why prefer the nullable `boolean` dtype over `object` or float? - How do `&`, `|` and boolean filtering behave on a `boolean` Series that contains `pd.NA`?

Overview: Write a pandas function that converts a Series of True and False strings, mixed with blanks, None and junk values, into a nullable boolean Series that keeps the index and name. It tests explicit value mapping, the pandas boolean extension dtype and careful handling of missing data.

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

Implement def to_nullable_bool(s: pd.Series) -> pd.Series that converts an object Series of 'True'/'False' strings, possibly mixed with missing markers (empty strings, None, other junk), into a nullable boolean Series.

Requirements:

  • Map 'True' to True , 'False' to False , and anything else (missing or unrecognised) to pd.NA .
  • Return dtype boolean (the nullable extension type, lowercase), not bool or object .
  • Preserve the input index and name .

Example:

s = pd.Series(['True', 'False', '', None, 'maybe'], name='flag')
to_nullable_bool(s)
# 0     True
# 1    False
# 2     <NA>
# 3     <NA>
# 4     <NA>
# Name: flag, dtype: boolean

What a Strong Answer Covers Guidance

  • An allow-list of the exact tokens 'True' and 'False' , so that every other value becomes missing.
  • The cast to the nullable boolean dtype, and why NumPy bool cannot represent the missing entries.
  • Preservation of the index and the name.
  • Behavior on None , NaN , empty strings, unknown strings, non-string objects and an empty Series.
  • A vectorized, linear-time implementation.

Follow-up Questions Guidance

  • How would you handle case-insensitive or padded inputs like ' true ' ?
  • Why prefer the nullable boolean dtype over object or float?
  • How do & , | and boolean filtering behave on a boolean Series that contains pd.NA ?