Build a five-step text normalization pipeline on a pandas Series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `clean_text(s: pd.Series) -> pd.Series`. It takes a Series of free-text strings, which may contain `NaN`, and normalizes it by applying these steps in this exact order:
1. Strip leading and trailing whitespace with `str.strip`.
2. Lowercase all text.
3. Collapse whitespace runs to one space: `replace(r'\s+', ' ', regex=True)`.
4. Remove punctuation: `replace(r'[^\w\s]', '', regex=True)`.
5. Apply a final `str.strip`.
```python
s = pd.Series([' Hello, World!! ', 'FOO...bar'])
clean_text(s)
# expected: pd.Series(['hello world', 'foobar'])
```
Punctuation is removed in place, so `'FOO...bar'` becomes `'foobar'` with no space inserted.
```hint Use the string accessor
Every step maps to a `.str` method, so you can chain them on the whole Series without writing a loop.
```
```hint Trace by hand
Write down the string after each step for both examples, and pay attention to what is left where a punctuation mark sat next to a space.
```
### What a Strong Answer Covers
- Implements the five steps in the stated order with vectorized `.str` methods and no row-by-row Python loop.
- Traces both examples through the steps and shows the output matches, including why `'FOO...bar'` gets no space.
- Explains what happens to missing values, the index and the Series `name`.
- Identifies which inputs are sensitive to the step order and why the final strip is needed.
- Points out what the punctuation regex does to ordinary text such as contractions, hyphenated words and numbers.
### Follow-up Questions
- Is there an input for which this pipeline leaves two consecutive spaces in the output? How would you reorder the steps to prevent it?
- How does the function treat `NaN`, non-string values in an object Series, and the original index and `name`?
- How would you adapt the pipeline for text where apostrophes, hyphens or decimal points carry meaning?
Overview: Implement a text cleaning function on a pandas Series that strips, lowercases, collapses whitespace, removes punctuation and strips again, in a fixed order. It tests vectorized string methods, regex replacement, missing-value handling and reasoning about how the order of cleaning steps changes the output.