Downcast an int64 pandas Series to the smallest signed integer type

Quick Overview

Write a pandas function that shrinks an int64 Series of values from 0 to 200 to the smallest safe signed integer dtype while keeping its index and name. It tests how pd.to_numeric downcasting chooses a type, the ranges of signed integer types, and the memory and overflow trade-offs of narrow dtypes.

Downcast an int64 pandas Series to the smallest signed integer type

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `downcast_ints(s: pd.Series) -> pd.Series`, which shrinks an `int64` Series to the smallest safe **signed** integer type using `pd.to_numeric(s, downcast='integer')`. Since 200 exceeds the `int8` maximum of 127, the result must be `int16`. The function must meet these requirements: - Return dtype `int16` for the given range of 0 to 200. - Downcast with `pd.to_numeric(..., downcast='integer')`. Do not hardcode the target type with `.astype()`. - Preserve the original `index` and `name`. Example: ```python s = pd.Series([0, 5, 127, 200], name="count") out = downcast_ints(s) out.dtype # dtype('int16') out.name # 'count' ``` ```hint Compare the ranges List the signed integer types from narrowest to widest with their minimum and maximum, then compare them with the smallest and largest value in the Series. ``` ### Constraints & Assumptions - The input is an `int64` Series. - All values are between 0 and 200, and there are no missing values. ### What a Strong Answer Covers - A call to `pd.to_numeric` with `downcast='integer'` and why it is safer than a hardcoded `astype`. - The ranges of the signed integer types and how the data's minimum and maximum decide the target dtype. - That the index and name carry over and the input Series is left unchanged. - The memory saved, and the conditions under which an integer downcast applies (no NaN, every value fits). ### Follow-up Questions - Why does the signed downcast pick `int16` instead of `uint8`, and how would you get `uint8`? - What happens if the Series contains NaN or negative values? - How much memory does the downcast save on a column of 10 million values, and how would you measure it? - What can go wrong when you do arithmetic on the downcast Series later?

Overview: Write a pandas function that shrinks an int64 Series of values from 0 to 200 to the smallest safe signed integer dtype while keeping its index and name. It tests how pd.to_numeric downcasting chooses a type, the ranges of signed integer types, and the memory and overflow trade-offs of narrow dtypes.

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

Implement downcast_ints(s: pd.Series) -> pd.Series, which shrinks an int64 Series to the smallest safe signed integer type using pd.to_numeric(s, downcast='integer'). Since 200 exceeds the int8 maximum of 127, the result must be int16.

The function must meet these requirements:

  • Return dtype int16 for the given range of 0 to 200.
  • Downcast with pd.to_numeric(..., downcast='integer') . Do not hardcode the target type with .astype() .
  • Preserve the original index and name .

Example:

s = pd.Series([0, 5, 127, 200], name="count")
out = downcast_ints(s)
out.dtype   # dtype('int16')
out.name    # 'count'

Constraints & Assumptions

  • The input is an int64 Series.
  • All values are between 0 and 200, and there are no missing values.

What a Strong Answer Covers Guidance

  • A call to pd.to_numeric with downcast='integer' and why it is safer than a hardcoded astype .
  • The ranges of the signed integer types and how the data's minimum and maximum decide the target dtype.
  • That the index and name carry over and the input Series is left unchanged.
  • The memory saved, and the conditions under which an integer downcast applies (no NaN, every value fits).

Follow-up Questions Guidance

  • Why does the signed downcast pick int16 instead of uint8 , and how would you get uint8 ?
  • What happens if the Series contains NaN or negative values?
  • How much memory does the downcast save on a column of 10 million values, and how would you measure it?
  • What can go wrong when you do arithmetic on the downcast Series later?