Downcast DataFrame To Shrink Memory

Quick Overview

Write a pandas function that downcasts an int64 count column and a float64 column to the smallest dtypes that keep their values, preserving the index, column names and order. It tests numeric dtype ranges, float precision, column assignment and how to measure DataFrame memory use.

Downcast DataFrame To Shrink Memory

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def shrink_frame(df: pd.DataFrame) -> pd.DataFrame` that reduces memory of a clean numeric DataFrame without changing values. The input has exactly two columns: an `int64` column of counts (values 0 to 100) and a `float64` column. Return a new downcast frame. Requirements: - Integer column via `pd.to_numeric(col, downcast='integer')` must become `int8`. - Float column via `pd.to_numeric(col, downcast='float')` must become `float32`. - Preserve index, column names, and column order. - Values stay unchanged; only the dtypes shrink. Example: ```python df = pd.DataFrame({"count": [0, 50, 100], "value": [1.5, 2.5, 3.5]}) # df.dtypes -> count: int64, value: float64 out = shrink_frame(df) # out.dtypes -> count: int8, value: float32 ``` ```hint Check the ranges Compare the value range of each column with the range and precision of the smaller types. Then think about how you put a converted column back into the frame so that its new dtype actually sticks. ``` ### Constraints & Assumptions - The frame has exactly two columns: an `int64` count column with values 0 to 100 and a `float64` column. - There are no missing values. - The float values survive a round trip through `float32` unchanged, as `1.5`, `2.5` and `3.5` do in the example. - The input frame must not be modified. ### What a Strong Answer Covers - Why the count column fits the smallest signed integer type without overflow - What a `float64` to `float32` conversion keeps and what it can lose - How the converted columns go back into a frame with the same index, names and order - How to confirm the result: the dtypes, the unchanged values and the memory saved ### Follow-up Questions - Why prefer `downcast='float'` over hard-casting to `float32`? - How would you generalize this to an arbitrary wide frame? - What changes if the count column can contain missing values?

Overview: Write a pandas function that downcasts an int64 count column and a float64 column to the smallest dtypes that keep their values, preserving the index, column names and order. It tests numeric dtype ranges, float precision, column assignment and how to measure DataFrame memory use.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement def shrink_frame(df: pd.DataFrame) -> pd.DataFrame that reduces memory of a clean numeric DataFrame without changing values. The input has exactly two columns: an int64 column of counts (values 0 to 100) and a float64 column. Return a new downcast frame.

Requirements:

  • Integer column via pd.to_numeric(col, downcast='integer') must become int8 .
  • Float column via pd.to_numeric(col, downcast='float') must become float32 .
  • Preserve index, column names, and column order.
  • Values stay unchanged; only the dtypes shrink.

Example:

df = pd.DataFrame({"count": [0, 50, 100], "value": [1.5, 2.5, 3.5]})
# df.dtypes -> count: int64, value: float64
out = shrink_frame(df)
# out.dtypes -> count: int8, value: float32

Constraints & Assumptions

  • The frame has exactly two columns: an int64 count column with values 0 to 100 and a float64 column.
  • There are no missing values.
  • The float values survive a round trip through float32 unchanged, as 1.5 , 2.5 and 3.5 do in the example.
  • The input frame must not be modified.

What a Strong Answer Covers Guidance

  • Why the count column fits the smallest signed integer type without overflow
  • What a float64 to float32 conversion keeps and what it can lose
  • How the converted columns go back into a frame with the same index, names and order
  • How to confirm the result: the dtypes, the unchanged values and the memory saved

Follow-up Questions Guidance

  • Why prefer downcast='float' over hard-casting to float32 ?
  • How would you generalize this to an arbitrary wide frame?
  • What changes if the count column can contain missing values?