Cast one pandas column to a given dtype without mutating the DataFrame
Quick Overview
Write a pandas function that casts one column of numeric strings to a named dtype such as int64 and returns a new DataFrame. It tests astype, leaving the caller's frame unmodified, keeping the other columns and the index intact, and handling values that cannot be parsed.
Cast one pandas column to a given dtype without mutating the DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `cast_column(df, col, dtype)` returning a copy of `df` with column `col` (numeric strings like `'10'`) cast to the numpy dtype named by `dtype` (e.g. `'int64'`, `'float64'`).
The result must meet these requirements:
- Cast only `col`: every other column and the index stay unchanged.
- Do not mutate `df`. The caller's frame keeps its original values and dtypes.
- The result must pass `assert_frame_equal` against the expected frame.
Example:
```python
df = pd.DataFrame({'a': ['10', '20'], 'b': [1, 2]})
out = cast_column(df, 'a', 'int64')
# out:
# a b
# 0 10 1
# 1 20 2
# out['a'].dtype -> int64
# df['a'].dtype -> object (df is unchanged)
```
```hint Leave the input alone
Assigning into `df[col]` changes the caller's frame. Look for a pandas method that hands back a new frame with one column replaced or recast.
```
### What a Strong Answer Covers
- The pandas call that turns the strings into the requested dtype, and what it does with a string it cannot parse.
- How the function avoids modifying the caller's frame.
- Why the other columns, the column order and the index come through unchanged.
- Edge cases such as missing values, a column that does not exist, and column labels that are not strings.
### Follow-up Questions
- What happens if the column holds `'10.5'` or a missing value and you ask for `'int64'`? How would you handle those inputs?
- How would you cast several columns at once, each to its own dtype?
- When would you reach for `pd.to_numeric` over `astype`?
Overview: Write a pandas function that casts one column of numeric strings to a named dtype such as int64 and returns a new DataFrame. It tests astype, leaving the caller's frame unmodified, keeping the other columns and the index intact, and handling values that cannot be parsed.
Implement cast_column(df, col, dtype) returning a copy of df with column col (numeric strings like '10') cast to the numpy dtype named by dtype (e.g. 'int64', 'float64').
The result must meet these requirements:
Cast only
col
: every other column and the index stay unchanged.
Do not mutate
df
. The caller's frame keeps its original values and dtypes.
The result must pass
assert_frame_equal
against the expected frame.
Example:
df = pd.DataFrame({'a': ['10', '20'], 'b': [1, 2]})
out = cast_column(df, 'a', 'int64')
# out:
# a b
# 0 10 1
# 1 20 2
# out['a'].dtype -> int64
# df['a'].dtype -> object (df is unchanged)
What a Strong Answer Covers Guidance
The pandas call that turns the strings into the requested dtype, and what it does with a string it cannot parse.
How the function avoids modifying the caller's frame.
Why the other columns, the column order and the index come through unchanged.
Edge cases such as missing values, a column that does not exist, and column labels that are not strings.
Follow-up Questions Guidance
What happens if the column holds
'10.5'
or a missing value and you ask for
'int64'
? How would you handle those inputs?
How would you cast several columns at once, each to its own dtype?
When would you reach for
pd.to_numeric
over
astype
?