Return a pandas boolean mask of rows whose status is in an allowed set

Quick Overview

Write a pandas function that returns a boolean mask marking orders whose status is one of three allowed values, using exact case-sensitive matching. It tests vectorized membership checks, the index, name and dtype of the result, and leaving the input frame unchanged.

Return a pandas boolean mask of rows whose status is in an allowed set

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `def valid_status_mask(df: pd.DataFrame) -> pd.Series`. The frame has columns `['order_id', 'status']`. Return a boolean mask that is `True` exactly where `status` is in `{'pending', 'shipped', 'delivered'}`. The mask must meet these requirements: - The match is case-sensitive and exact: `'PENDING'` and `'lost'` are invalid. - The result has dtype `bool`, is aligned to `df.index`, and is named `'status'`. - `df` is not mutated. Example: ```python df = pd.DataFrame({ 'order_id': [1, 2, 3, 4], 'status': ['pending', 'PENDING', 'delivered', 'lost'], }) valid_status_mask(df) # 0 True # 1 False # 2 True # 3 False # Name: status, dtype: bool ``` Rows 0 and 2 hold allowed values exactly. Row 1 differs only in case and row 3 is not in the set, so both are `False`. ```hint Vectorized membership Skip the Python loop: pandas can test every value of a column against a collection in one call. Check which name and dtype that call gives its result. ``` ### What a Strong Answer Covers - An exact, case-sensitive, vectorized membership test against the three allowed values. - Why the result has dtype `bool`, `df`'s index and the name `'status'`. - Leaving `df` unchanged. - How missing values and near misses (other casing, stray whitespace) are treated. ### Follow-up Questions - How would you make the check case-insensitive while preserving the original values? - How would you report which invalid values appear and how often? - How could the column's dtype enforce the allowed set so that new invalid values cannot be written into it?

Overview: Write a pandas function that returns a boolean mask marking orders whose status is one of three allowed values, using exact case-sensitive matching. It tests vectorized membership checks, the index, name and dtype of the result, and leaving the input frame unchanged.

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

Write def valid_status_mask(df: pd.DataFrame) -> pd.Series. The frame has columns ['order_id', 'status']. Return a boolean mask that is True exactly where status is in {'pending', 'shipped', 'delivered'}.

The mask must meet these requirements:

  • The match is case-sensitive and exact: 'PENDING' and 'lost' are invalid.
  • The result has dtype bool , is aligned to df.index , and is named 'status' .
  • df is not mutated.

Example:

df = pd.DataFrame({
    'order_id': [1, 2, 3, 4],
    'status':   ['pending', 'PENDING', 'delivered', 'lost'],
})
valid_status_mask(df)
# 0     True
# 1    False
# 2     True
# 3    False
# Name: status, dtype: bool

Rows 0 and 2 hold allowed values exactly. Row 1 differs only in case and row 3 is not in the set, so both are False.

What a Strong Answer Covers Guidance

  • An exact, case-sensitive, vectorized membership test against the three allowed values.
  • Why the result has dtype bool , df 's index and the name 'status' .
  • Leaving df unchanged.
  • How missing values and near misses (other casing, stray whitespace) are treated.

Follow-up Questions Guidance

  • How would you make the check case-insensitive while preserving the original values?
  • How would you report which invalid values appear and how often?
  • How could the column's dtype enforce the allowed set so that new invalid values cannot be written into it?