Convert a Float Series with Missing Values to Nullable Int64 in pandas
Quick Overview
Convert a pandas float64 Series of whole numbers with missing values into the nullable Int64 dtype, keeping the index and name and turning NaN into pd.NA. It tests why NumPy int64 cannot store missing data and how the masked integer type and pd.NA behave.
Convert a Float Series with Missing Values to Nullable Int64 in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `to_nullable_int(s: pd.Series) -> pd.Series`. The input is a `float64` Series that holds whole numbers and missing values (`np.nan`); it is stored as float only because of the gaps. Return the values as integers with the missing entries still marked as missing.
Requirements:
- The result has pandas' nullable `Int64` dtype, and every `np.nan` becomes `pd.NA`.
- Preserve the index and `name`. The output is checked with `assert_series_equal`.
Example:
```python
s = pd.Series([1.0, 2.0, np.nan, 4.0])
out = to_nullable_int(s)
# pd.Series([1, 2, pd.NA, 4], dtype='Int64')
```
The values 1, 2 and 4 come back as integers, and the missing value at position 2 becomes `pd.NA`.
```hint Why is it float?
Ask why a column of whole numbers ended up as `float64` in the first place, then compare what each integer dtype in pandas does with a missing value.
```
### What a Strong Answer Covers
- Why a NumPy `int64` array cannot store a missing value, and why a single NaN keeps integer data in `float64`.
- The conversion to the nullable `Int64` dtype with the index and name unchanged.
- How the nullable dtype stores its data and how `pd.NA` behaves once the conversion is done.
- What happens when a non-whole value such as 2.5 is present, and how to validate or round before casting.
### Follow-up Questions
- Why can't a plain `int64` dtype hold these values?
- What happens if a value like `2.5` is present?
- How does `pd.NA` behave in comparisons and `if` statements, compared with `np.nan`?
- How do you pass an `Int64` column to a library that expects a plain NumPy array?
Overview: Convert a pandas float64 Series of whole numbers with missing values into the nullable Int64 dtype, keeping the index and name and turning NaN into pd.NA. It tests why NumPy int64 cannot store missing data and how the masked integer type and pd.NA behave.
Write to_nullable_int(s: pd.Series) -> pd.Series. The input is a float64 Series that holds whole numbers and missing values (np.nan); it is stored as float only because of the gaps. Return the values as integers with the missing entries still marked as missing.
Requirements:
The result has pandas' nullable
Int64
dtype, and every
np.nan
becomes
pd.NA
.
Preserve the index and
name
. The output is checked with
assert_series_equal
.
Example:
s = pd.Series([1.0, 2.0, np.nan, 4.0])
out = to_nullable_int(s)
# pd.Series([1, 2, pd.NA, 4], dtype='Int64')
The values 1, 2 and 4 come back as integers, and the missing value at position 2 becomes pd.NA.
What a Strong Answer Covers Guidance
Why a NumPy
int64
array cannot store a missing value, and why a single NaN keeps integer data in
float64
.
The conversion to the nullable
Int64
dtype with the index and name unchanged.
How the nullable dtype stores its data and how
pd.NA
behaves once the conversion is done.
What happens when a non-whole value such as 2.5 is present, and how to validate or round before casting.
Follow-up Questions Guidance
Why can't a plain
int64
dtype hold these values?
What happens if a value like
2.5
is present?
How does
pd.NA
behave in comparisons and
if
statements, compared with
np.nan
?
How do you pass an
Int64
column to a library that expects a plain NumPy array?