Downcast an int64 pandas Series to the smallest unsigned integer type (uint8)

Quick Overview

Write a pandas function that converts an int64 Series with values from 0 to 255 into the smallest unsigned integer dtype while keeping its values, index and name. It tests how pd.to_numeric picks an unsigned type and what happens with negative values, larger values and floats.

Downcast an int64 pandas Series to the smallest unsigned integer type (uint8)

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `downcast_numeric(s: pd.Series) -> pd.Series`. The input is an `int64` Series with all values from 0 to 255. Return the same values in the smallest unsigned integer dtype (`uint8`). The function must meet these requirements: - Produce the result with `pd.to_numeric(s, downcast='unsigned')`. - Preserve the original `index` and `name`. - Leave the values unchanged. Only the dtype may change (for example `int64` to `uint8`). Example: ```python s = pd.Series([0, 127, 255], name="x") downcast_numeric(s) # 0 0 # 1 127 # 2 255 # Name: x, dtype: uint8 ``` ```hint Check both ends of the range Compare the Series' minimum and maximum with the ranges of `uint8`, `uint16`, `uint32` and `uint64`, and ask what the minimum has to satisfy before any unsigned type is usable. ``` ### Constraints & Assumptions - The input is an `int64` Series, so it has no missing values. - Every value is between 0 and 255 inclusive. ### What a Strong Answer Covers - A call to `pd.to_numeric` with `downcast='unsigned'` and how it chooses among `uint8`, `uint16`, `uint32` and `uint64`. - Why the range 0 to 255 lands on `uint8`, and what role the non-negative minimum plays. - That the values, index and name come through unchanged and the input Series is not modified. - What happens to inputs outside the assumptions: values above 255, negative values and floats. ### Follow-up Questions - How does `downcast='unsigned'` differ from `downcast='integer'`, and which would you choose for this data? - What happens if a value exceeds 255, or if the Series holds floats? - What does the function return if one value is negative? - How would you apply the same downcast to every integer column of a DataFrame?

Overview: Write a pandas function that converts an int64 Series with values from 0 to 255 into the smallest unsigned integer dtype while keeping its values, index and name. It tests how pd.to_numeric picks an unsigned type and what happens with negative values, larger values and floats.

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

Implement downcast_numeric(s: pd.Series) -> pd.Series. The input is an int64 Series with all values from 0 to 255. Return the same values in the smallest unsigned integer dtype (uint8).

The function must meet these requirements:

  • Produce the result with pd.to_numeric(s, downcast='unsigned') .
  • Preserve the original index and name .
  • Leave the values unchanged. Only the dtype may change (for example int64 to uint8 ).

Example:

s = pd.Series([0, 127, 255], name="x")
downcast_numeric(s)
# 0      0
# 1    127
# 2    255
# Name: x, dtype: uint8

Constraints & Assumptions

  • The input is an int64 Series, so it has no missing values.
  • Every value is between 0 and 255 inclusive.

What a Strong Answer Covers Guidance

  • A call to pd.to_numeric with downcast='unsigned' and how it chooses among uint8 , uint16 , uint32 and uint64 .
  • Why the range 0 to 255 lands on uint8 , and what role the non-negative minimum plays.
  • That the values, index and name come through unchanged and the input Series is not modified.
  • What happens to inputs outside the assumptions: values above 255, negative values and floats.

Follow-up Questions Guidance

  • How does downcast='unsigned' differ from downcast='integer' , and which would you choose for this data?
  • What happens if a value exceeds 255, or if the Series holds floats?
  • What does the function return if one value is negative?
  • How would you apply the same downcast to every integer column of a DataFrame?