How can you reduce Pandas DataFrame memory with category, nullable and PyArrow-backed dtypes?

Quick Overview

Asks how to shrink the memory footprint of a large Pandas DataFrame by choosing better column dtypes. Tests measuring real memory use, numeric downcasting, categorical encoding, nullable integer and boolean types, PyArrow-backed strings, and the trade-offs each choice brings.

How can you reduce Pandas DataFrame memory with category, nullable and PyArrow-backed dtypes?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How can you optimize memory usage in Pandas DataFrames, particularly by choosing appropriate dtypes like `category`, nullable dtypes, or PyArrow-backed dtypes? Assume a large DataFrame loaded from a CSV with numeric columns, repeated string labels, free text, and integer columns that have some missing values. ```hint Where the bytes go Think about what an `object` column of strings stores for every row, and what changes when that column has only a few distinct values. ``` ```hint Missing values Check what dtype an integer column gets when a single value is missing, and what that does to its size. ``` ### What a Strong Answer Covers - How to measure real memory use, including why `object` columns need `deep=True` - When downcasting a numeric column is safe and what it costs in range and precision - How `category` stores data and the cardinality at which it stops paying off - What nullable and PyArrow-backed dtypes change for missing values and for strings - Applying dtypes at load time so the wide version of the frame is never built ### Follow-up Questions - You concatenate two frames whose `category` columns have different categories. What dtype comes out, and how do you prevent it? - When would you keep a column as `float64` even though `float32` covers its range? - The CSV is 30 GB and still does not fit in memory with lean dtypes. What do you do next? - What can break or slow down when a whole pipeline switches to `dtype_backend="pyarrow"`?

Overview: Asks how to shrink the memory footprint of a large Pandas DataFrame by choosing better column dtypes. Tests measuring real memory use, numeric downcasting, categorical encoding, nullable integer and boolean types, PyArrow-backed strings, and the trade-offs each choice brings.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How can you optimize memory usage in Pandas DataFrames, particularly by choosing appropriate dtypes like category, nullable dtypes, or PyArrow-backed dtypes?

Assume a large DataFrame loaded from a CSV with numeric columns, repeated string labels, free text, and integer columns that have some missing values.

What a Strong Answer Covers Guidance

  • How to measure real memory use, including why object columns need deep=True
  • When downcasting a numeric column is safe and what it costs in range and precision
  • How category stores data and the cardinality at which it stops paying off
  • What nullable and PyArrow-backed dtypes change for missing values and for strings
  • Applying dtypes at load time so the wide version of the frame is never built

Follow-up Questions Guidance

  • You concatenate two frames whose category columns have different categories. What dtype comes out, and how do you prevent it?
  • When would you keep a column as float64 even though float32 covers its range?
  • The CSV is 30 GB and still does not fit in memory with lean dtypes. What do you do next?
  • What can break or slow down when a whole pipeline switches to dtype_backend="pyarrow" ?