How can you reduce a DataFrame's memory usage through dtype downcasting?

Quick Overview

Explain how to shrink a pandas DataFrame by downcasting numeric columns to smaller integer and float types and converting repetitive strings to category. Tests memory measurement, safe range and precision checks, and the overflow and cardinality pitfalls of smaller dtypes.

How can you reduce a DataFrame's memory usage through dtype downcasting?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How can you reduce a DataFrame's memory usage through dtype downcasting? Suppose a pandas DataFrame with tens of millions of rows barely fits in memory, and every column was loaded with the default dtypes. ```hint Look at the defaults Check which dtype pandas gives integers, floats and strings by default, and compare the bytes per row with what the values in each column actually need. ``` ### What a Strong Answer Covers - How to measure memory per column, including the cost of string contents - Picking the smallest safe numeric type, signed or unsigned, and how the range gets checked - When converting text columns to `category` pays off and when it does not - Precision and overflow risks that come with smaller types - Setting dtypes at load time so the large version never exists ### Follow-up Questions - An integer column has missing values and loaded as `float64`. How do you shrink it without losing the missing values? - Why can turning a user id column into `category` make memory usage worse? - After downcasting, a column computed as `a - b` shows huge positive numbers. What happened? - How do you keep the savings when you concatenate many files whose categorical columns have different categories?

Overview: Explain how to shrink a pandas DataFrame by downcasting numeric columns to smaller integer and float types and converting repetitive strings to category. Tests memory measurement, safe range and precision checks, and the overflow and cardinality pitfalls of smaller dtypes.

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

How can you reduce a DataFrame's memory usage through dtype downcasting?

Suppose a pandas DataFrame with tens of millions of rows barely fits in memory, and every column was loaded with the default dtypes.

What a Strong Answer Covers Guidance

  • How to measure memory per column, including the cost of string contents
  • Picking the smallest safe numeric type, signed or unsigned, and how the range gets checked
  • When converting text columns to category pays off and when it does not
  • Precision and overflow risks that come with smaller types
  • Setting dtypes at load time so the large version never exists

Follow-up Questions Guidance

  • An integer column has missing values and loaded as float64 . How do you shrink it without losing the missing values?
  • Why can turning a user id column into category make memory usage worse?
  • After downcasting, a column computed as a - b shows huge positive numbers. What happened?
  • How do you keep the savings when you concatenate many files whose categorical columns have different categories?