Parse Comma-Thousands Strings to Nullable Int64 in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `def parse_thousands_to_Int64(s: pd.Series) -> pd.Series` that converts an object Series of integer strings with comma thousands separators into the nullable `Int64` type. Blank strings become missing values.
The function must:
- Remove commas, parse with `pd.to_numeric(errors='coerce')`, then `astype('Int64')`.
- Return dtype `Int64`, not float or object.
- Turn blank strings into `pd.NA`, not `np.nan`.
- Preserve the input `index` and `name`.
Example:
```python
s = pd.Series(['1,234', '56', '', '9,000'])
parse_thousands_to_Int64(s)
# 0 1234
# 1 56
# 2 <NA>
# 3 9000
# dtype: Int64
```
```hint Watch the middle step
Check the dtype `pd.to_numeric` returns once a blank has been coerced, and what that dtype can and cannot represent exactly.
```
```hint Follow the blank
Trace the empty string through each step and note which step is the one that produces `pd.NA`.
```
### What a Strong Answer Covers
- Why the commas have to go before parsing, and why a literal replacement (`regex=False`) is enough.
- What `errors='coerce'` does to the blank string, and which dtype `pd.to_numeric` returns as a result.
- How the `Int64` cast maps `NaN` to `pd.NA`, and why a plain `int64` column cannot hold missing values.
- That every step preserves the index and name.
- Inputs outside the happy path: whitespace, negative numbers, decimal strings, non-string elements and very large integers.
### Follow-up Questions
- Why does `Int64` use `pd.NA` instead of `np.nan`?
- How would you also handle negative numbers and surrounding whitespace?
- In a column that also has blanks, what happens to an ID like `'9,007,199,254,740,993'`, and how would you parse it exactly?
Overview: Write a pandas function that turns strings with comma thousands separators into a nullable Int64 Series, with blank strings becoming missing values. Tests pd.to_numeric coercion, nullable integer dtypes and pd.NA, and inputs such as whitespace, decimals and integers too large for float64.