Coerce Numeric Strings to Floats in Pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def coerce_numeric(s: pd.Series) -> pd.Series` that converts an object `Series` of numeric-looking strings mixed with garbage (e.g. `'abc'`, `''`) into numbers. Unparseable entries become `NaN`.
Requirements:
- Use `pd.to_numeric(s, errors='coerce')`.
- The returned dtype must be `float64`.
- Preserve the original index and Series `name`.
Example:
```python
s = pd.Series(['10', '20', 'abc', '', '3.5'], name='vals')
coerce_numeric(s)
# 0 10.0
# 1 20.0
# 2 NaN
# 3 NaN
# 4 3.5
# Name: vals, dtype: float64
```
```hint Test more than the example
Run your function on a Series where every string is a clean integer, such as `['1', '2', '3']`, and check the dtype it returns.
```
### What a Strong Answer Covers
- What `errors='coerce'` does with a bad value compared with the default `errors='raise'`.
- How the answer guarantees `float64` even when no value fails to parse.
- Why the output keeps the index and `name` of the input.
- Which strings become `NaN` (words, empty strings, thousands separators, currency symbols) and which parse even though they may surprise you.
- The cost of the vectorized call compared with a row-by-row Python loop.
### Follow-up Questions
- Values such as `'1,200'`, `'$5'` and `'12%'` should count as numbers. How do you clean them before converting?
- How would you list the raw values that failed to parse so the data owner can fix them?
- The column holds integer IDs larger than ${2^{53}}$. What goes wrong with `float64`, and which dtype would you use instead?
- How would you keep whole numbers as integers while still marking failures as missing?
Overview: Write a pandas function that converts a Series of numeric-looking strings mixed with garbage values into floats, turning anything unparseable into a missing value. It tests pandas numeric parsing, how the result dtype is chosen, and keeping the index and name of the input intact.