Data Cleaning Exercises

Module 8 · Types and identifiers · 6 min

Nullable booleans

You'll be able to: Store yes/no/unknown with a type that keeps unknown distinct.

Before this: Parse booleans from an approved list

The idea

A boolean with three states (true, false, unknown) needs a type that allows missing values: BOOLEAN in SQL, boolean in pandas.

Plain bool in pandas can't hold missing values, so conversions may turn unknown into False.

Worked example

Input: opted_in: true, false, (unknown)

Visual
Convert type → bool (missing kept)
SQL (DuckDB)
CAST(opted_in AS BOOLEAN)
pandas
s.astype("boolean")

Result: true, false, <NA>

Common mistakes

  • Using plain bool.
  • Treating unknown as False.

Check yourself

What pandas dtype keeps missing booleans?

boolean (nullable).

Practice it

Parse yes/no flags.

Next: Keep identifiers as text and parse yes/no flags