Build a boolean missing-value mask for a pandas DataFrame
Quick Overview
Write a pandas function that returns a boolean mask marking every missing cell of a DataFrame while keeping its shape, index and columns. It tests how pandas represents missing values across dtypes and why comparing values against NaN goes wrong.
Build a boolean missing-value mask for a pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def missing_mask(df: pd.DataFrame) -> pd.DataFrame` that returns a boolean mask of `df`. Each cell of the mask is `True` exactly when the corresponding input cell is missing, as reported by `df.isna()`.
The function must meet these requirements:
- The result has the same shape, columns and index as `df`.
- Every column of the result has dtype `bool`.
- A cell is `True` if and only if the input cell is missing (`NaN`, `None` or `NaT`).
- An empty DataFrame returns an empty boolean DataFrame with the same columns.
- `df` is not modified in place.
Example:
```python
df = pd.DataFrame({"a": [1, None], "b": ["x", "y"]})
missing_mask(df)
# a b
# 0 False False
# 1 True False
```
```hint Comparing with NaN
Check what `np.nan == np.nan` returns before you try to find missing cells by comparing values.
```
### What a Strong Answer Covers
- A vectorized approach that returns a new frame and keeps the original index and column labels.
- Which values pandas treats as missing in each kind of column (float, object, datetime, nullable extension types).
- Why an equality test against `np.nan` or an `is None` check produces the wrong mask.
- The empty-frame case and the guarantee that the input stays unchanged.
### Follow-up Questions
- How would you report the share of missing values per column and list the rows that have at least one missing value?
- The raw data marks gaps with sentinels such as `-999`, `""` or `"N/A"`. How would you include them in the mask?
- Are `np.inf` and empty strings missing under `isna()`? What would you change if the team wants them counted?
- How do nullable dtypes such as `Int64` and `string` store missing values, and does your mask still work on them?
Overview: Write a pandas function that returns a boolean mask marking every missing cell of a DataFrame while keeping its shape, index and columns. It tests how pandas represents missing values across dtypes and why comparing values against NaN goes wrong.
Implement def missing_mask(df: pd.DataFrame) -> pd.DataFrame that returns a boolean mask of df. Each cell of the mask is True exactly when the corresponding input cell is missing, as reported by df.isna().
The function must meet these requirements:
The result has the same shape, columns and index as
df
.
Every column of the result has dtype
bool
.
A cell is
True
if and only if the input cell is missing (
NaN
,
None
or
NaT
).
An empty DataFrame returns an empty boolean DataFrame with the same columns.
df
is not modified in place.
Example:
df = pd.DataFrame({"a": [1, None], "b": ["x", "y"]})
missing_mask(df)
# a b
# 0 False False
# 1 True False
What a Strong Answer Covers Guidance
A vectorized approach that returns a new frame and keeps the original index and column labels.
Which values pandas treats as missing in each kind of column (float, object, datetime, nullable extension types).
Why an equality test against
np.nan
or an
is None
check produces the wrong mask.
The empty-frame case and the guarantee that the input stays unchanged.
Follow-up Questions Guidance
How would you report the share of missing values per column and list the rows that have at least one missing value?
The raw data marks gaps with sentinels such as
-999
,
""
or
"N/A"
. How would you include them in the mask?
Are
np.inf
and empty strings missing under
isna()
? What would you change if the team wants them counted?
How do nullable dtypes such as
Int64
and
string
store missing values, and does your mask still work on them?