Collapse runs of whitespace and strip the ends in a pandas string Series

Quick Overview

Write a pandas function that collapses every run of spaces, tabs and newlines in a string Series into one space and strips both ends, leaving missing values, the index and the name intact. It tests vectorized string methods, regex whitespace classes and Unicode edge cases.

Collapse runs of whitespace and strip the ends in a pandas string Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `def collapse_whitespace(s: pd.Series) -> pd.Series` that normalizes the whitespace in every string value and returns a new Series. The function must meet these requirements: - Replace every run of internal whitespace (spaces, tabs, newlines) with a single space. - Strip leading and trailing whitespace. - Leave `NaN` as `NaN` (no coercion to `'nan'`). - Preserve the input `index` and `name`. Example: ```python s = pd.Series(['hello world', ' a\tb \n c ']) collapse_whitespace(s) # 0 hello world # 1 a b c # dtype: object ``` ```hint Runs, then ends Treat a run of mixed whitespace as one unit to replace, then handle what is left at the two ends of the string. ``` ```hint Missing values Check what the `.str` accessor returns for a missing entry before you add any type conversion. ``` ### Constraints & Assumptions - The Series has object or string dtype. ### What a Strong Answer Covers - A vectorized transformation that collapses mixed whitespace runs and strips both ends. - How missing values pass through untouched, and which common preprocessing step would turn them into the text `'nan'`. - Keeping the index and name of the input. - Which characters count as whitespace, including Unicode spaces such as the non-breaking space (U+00A0). ### Follow-up Questions - How would you also normalize non-breaking spaces (` `) and other Unicode whitespace? - Would this work on a large Series, and how could you speed it up? - What happens to an integer stored in an object column that is meant to hold strings? - Should a value that is only whitespace become an empty string or a missing value?

Overview: Write a pandas function that collapses every run of spaces, tabs and newlines in a string Series into one space and strips both ends, leaving missing values, the index and the name intact. It tests vectorized string methods, regex whitespace classes and Unicode edge cases.

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

Implement def collapse_whitespace(s: pd.Series) -> pd.Series that normalizes the whitespace in every string value and returns a new Series.

The function must meet these requirements:

  • Replace every run of internal whitespace (spaces, tabs, newlines) with a single space.
  • Strip leading and trailing whitespace.
  • Leave NaN as NaN (no coercion to 'nan' ).
  • Preserve the input index and name .

Example:

s = pd.Series(['hello   world', '  a\tb  \n c '])
collapse_whitespace(s)
# 0    hello world
# 1    a b c
# dtype: object

Constraints & Assumptions

  • The Series has object or string dtype.

What a Strong Answer Covers Guidance

  • A vectorized transformation that collapses mixed whitespace runs and strips both ends.
  • How missing values pass through untouched, and which common preprocessing step would turn them into the text 'nan' .
  • Keeping the index and name of the input.
  • Which characters count as whitespace, including Unicode spaces such as the non-breaking space (U+00A0).

Follow-up Questions Guidance

  • How would you also normalize non-breaking spaces (   ) and other Unicode whitespace?
  • Would this work on a large Series, and how could you speed it up?
  • What happens to an integer stored in an object column that is meant to hold strings?
  • Should a value that is only whitespace become an empty string or a missing value?