Filter a Pandas Series to Rows Containing 'error', Ignoring Case

Quick Overview

Write a pandas function that keeps only the Series rows whose text contains the substring error in any letter case. It tests vectorized string matching, safe handling of missing values in a boolean mask, and keeping the original index labels and Series name.

Filter a Pandas Series to Rows Containing 'error', Ignoring Case

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def filter_contains_error(s: pd.Series) -> pd.Series` that returns only the rows whose string value contains `'error'`, case-insensitively. The function must meet these requirements: - Match ignoring case (for example `'ERROR'` and `'Error'`). - Treat `NaN` and `None` as non-matching, and never raise on them. - Preserve the surviving rows' original index labels (no reset) and the Series `name`. Example: ```python import pandas as pd s = pd.Series(['ERROR: disk', 'ok', 'soft error', None]) filter_contains_error(s) # 0 ERROR: disk # 2 soft error # dtype: object ``` The printed output assumes pandas 2.x, where a list of strings becomes an `object` Series. ```hint Missing values Look at what your string test returns for the `None` row, and at what boolean indexing does when the mask is not purely `True` and `False`. ``` ### What a Strong Answer Covers - A vectorized, case-insensitive substring test applied to every row. - How missing values are handled so the mask is purely boolean and the call never raises. - A selection that keeps the original index labels and the Series name. - Whether the search term is read as a regular expression, and what that means for other search terms. - What counts as a match, including substrings inside longer words and non-string entries. ### Follow-up Questions - How does `regex=True` (the default) affect this, and when would you disable it? - How would you match only the whole word "error" and skip values such as "terror" or "errors"? - How would you apply the same filter to one column of a DataFrame and return the full rows? - What changes if the Series uses the nullable `string` dtype instead of `object`?

Overview: Write a pandas function that keeps only the Series rows whose text contains the substring error in any letter case. It tests vectorized string matching, safe handling of missing values in a boolean mask, and keeping the original index labels and Series name.

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

Implement def filter_contains_error(s: pd.Series) -> pd.Series that returns only the rows whose string value contains 'error', case-insensitively.

The function must meet these requirements:

  • Match ignoring case (for example 'ERROR' and 'Error' ).
  • Treat NaN and None as non-matching, and never raise on them.
  • Preserve the surviving rows' original index labels (no reset) and the Series name .

Example:

import pandas as pd

s = pd.Series(['ERROR: disk', 'ok', 'soft error', None])
filter_contains_error(s)
# 0    ERROR: disk
# 2     soft error
# dtype: object

The printed output assumes pandas 2.x, where a list of strings becomes an object Series.

What a Strong Answer Covers Guidance

  • A vectorized, case-insensitive substring test applied to every row.
  • How missing values are handled so the mask is purely boolean and the call never raises.
  • A selection that keeps the original index labels and the Series name.
  • Whether the search term is read as a regular expression, and what that means for other search terms.
  • What counts as a match, including substrings inside longer words and non-string entries.

Follow-up Questions Guidance

  • How does regex=True (the default) affect this, and when would you disable it?
  • How would you match only the whole word "error" and skip values such as "terror" or "errors"?
  • How would you apply the same filter to one column of a DataFrame and return the full rows?
  • What changes if the Series uses the nullable string dtype instead of object ?