Repair a Mixed-Type pandas Object Column Into Clean float64 Values
Quick Overview
Write a pandas function that turns an object column mixing native numbers, numeric strings with commas or stray spaces, and junk tokens into a clean float64 column. It tests selective string cleaning, coercion with pd.to_numeric, and keeping the dtype, index and name stable for every input.
Repair a Mixed-Type pandas Object Column Into Clean float64 Values
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
You are given an `object` Series that mixes native ints, floats, numeric strings (with stray whitespace and thousands separators) and non-numeric sentinel tokens. Write `repair_mixed_column(s)` that returns a clean numeric column.
```python
import pandas as pd
def repair_mixed_column(s: pd.Series) -> pd.Series:
...
```
Requirements:
- Strip leading and trailing whitespace and remove `,` thousands separators from string entries.
- Coerce the cleaned column with `pd.to_numeric(..., errors='coerce')`.
- Return dtype `float64`, with `np.nan` for unparseable entries (for example `'N/A'`).
- Preserve the original index and name.
Example:
```python
s = pd.Series([5, 3.0, ' 7 ', '1,000', 'N/A'])
repair_mixed_column(s)
# 0 5.0
# 1 3.0
# 2 7.0
# 3 1000.0
# 4 NaN
# dtype: float64
```
```hint Watch the native numbers
Before reaching for the `.str` accessor, check what it returns for the elements of this column that are not strings.
```
```hint Check the dtype on clean input
Run your function on a column where every entry parses as an integer and look at the dtype it returns.
```
### What a Strong Answer Covers
- Which entries need string cleaning and how the native ints and floats are kept intact
- How unparseable tokens such as `'N/A'` end up as `NaN`
- How the result is guaranteed to be `float64` for every input, including one where nothing fails to parse
- How the index and name carry through to the result
- Which inputs the cleaning rules would silently misread
### Follow-up Questions
- Why does the result come back as `float64` and not an integer type, and how would you keep integer values while still marking missing entries?
- How would you report which input values were turned into `NaN` by the coercion?
- How would you handle a source that writes numbers with a decimal comma, such as `'1.000,5'`?
Overview: Write a pandas function that turns an object column mixing native numbers, numeric strings with commas or stray spaces, and junk tokens into a clean float64 column. It tests selective string cleaning, coercion with pd.to_numeric, and keeping the dtype, index and name stable for every input.
You are given an object Series that mixes native ints, floats, numeric strings (with stray whitespace and thousands separators) and non-numeric sentinel tokens. Write repair_mixed_column(s) that returns a clean numeric column.
import pandas as pd
def repair_mixed_column(s: pd.Series) -> pd.Series:
...
Requirements:
Strip leading and trailing whitespace and remove
,
thousands separators from string entries.
Coerce the cleaned column with
pd.to_numeric(..., errors='coerce')
.
Return dtype
float64
, with
np.nan
for unparseable entries (for example
'N/A'
).