Upgrade a pandas DataFrame to nullable dtypes with convert_dtypes

Quick Overview

Convert a pandas DataFrame with legacy NumPy dtypes to nullable extension dtypes such as Int64, Float64, boolean and string. It tests how convert_dtypes infers each column's type, how missing values become pd.NA, and where that inference can surprise you.

Upgrade a pandas DataFrame to nullable dtypes with convert_dtypes

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `optimize_via_convert(df: pd.DataFrame) -> pd.DataFrame`. The input uses legacy NumPy-backed dtypes (`object` strings, `int64`, and `float64` with `NaN`). Return it upgraded to pandas nullable extension dtypes (`string`, `Int64`, `Float64`, `boolean`), with missing values represented as `pd.NA`. The function must: - Return the same frame as `df.convert_dtypes()`. The grader compares the two with `assert_frame_equal`. - Preserve the original index and column order. - Mutate `df` in place only if it still returns the converted frame. ```hint Look at the values Check each column's values as well as its current dtype. A `float64` column with `NaN` may hold nothing but whole numbers. ``` ### What a Strong Answer Covers - Which nullable dtype each kind of input column becomes, including float columns whose values are all whole numbers. - How `NaN` and `None` turn into `pd.NA`, and why an integer column can keep an integer dtype when it has gaps. - Why the index and column order come through unchanged. - Why the built-in method is safer than casting column by column when the grader checks dtypes exactly. ### Follow-up Questions - How do nullable dtypes differ from NumPy dtypes for missing data? - When might `convert_dtypes()` produce surprising results, and how would you control it? - What changes for downstream code once a column holds `pd.NA` instead of `NaN`?

Overview: Convert a pandas DataFrame with legacy NumPy dtypes to nullable extension dtypes such as Int64, Float64, boolean and string. It tests how convert_dtypes infers each column's type, how missing values become pd.NA, and where that inference can surprise you.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement optimize_via_convert(df: pd.DataFrame) -> pd.DataFrame. The input uses legacy NumPy-backed dtypes (object strings, int64, and float64 with NaN). Return it upgraded to pandas nullable extension dtypes (string, Int64, Float64, boolean), with missing values represented as pd.NA.

The function must:

  • Return the same frame as df.convert_dtypes() . The grader compares the two with assert_frame_equal .
  • Preserve the original index and column order.
  • Mutate df in place only if it still returns the converted frame.

What a Strong Answer Covers Guidance

  • Which nullable dtype each kind of input column becomes, including float columns whose values are all whole numbers.
  • How NaN and None turn into pd.NA , and why an integer column can keep an integer dtype when it has gaps.
  • Why the index and column order come through unchanged.
  • Why the built-in method is safer than casting column by column when the grader checks dtypes exactly.

Follow-up Questions Guidance

  • How do nullable dtypes differ from NumPy dtypes for missing data?
  • When might convert_dtypes() produce surprising results, and how would you control it?
  • What changes for downstream code once a column holds pd.NA instead of NaN ?