Zero-pad code strings to five characters in a pandas Series

Quick Overview

Left-pad code strings in a pandas Series with zeros to a width of five using the vectorized str.zfill, keeping NaN, the index and the name. Tests how string accessor methods treat missing values, longer strings, non-string elements and numeric input.

Zero-pad code strings to five characters in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def zero_pad_codes(s: pd.Series) -> pd.Series` that left-pads each code string with zeros to width 5 using the vectorized `s.str.zfill(5)` (no Python loop). The result must meet these requirements: - Leave values already 5+ characters unchanged. - Keep `NaN` as `NaN`. - Preserve the original index and `name`. Example: ```text Input: pd.Series(['7', '42', '12345']) Output: pd.Series(['00007', '00042', '12345']) ``` ```hint Check the odd values Before trusting the accessor, look at what `.str` methods return for a missing value and for an element that is not a string at all. ``` ### Constraints & Assumptions - The Series holds code strings, possibly with `NaN` for missing codes. - The target width is 5 characters. ### What a Strong Answer Covers - A vectorized call that pads only the values shorter than 5 characters. - How `NaN`, the index and the Series name come through the call. - What happens to elements that are not strings, or that carry a leading sign or whitespace. - How the approach changes when the input is numeric. ### Follow-up Questions - What if the input Series is numeric (`int64`) instead of strings? - What if the numeric column has missing values, so it arrives as `float64`? - How does `str.zfill(5)` differ from `str.pad(5, side='left', fillchar='0')`?

Overview: Left-pad code strings in a pandas Series with zeros to a width of five using the vectorized str.zfill, keeping NaN, the index and the name. Tests how string accessor methods treat missing values, longer strings, non-string elements and numeric input.

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

Implement def zero_pad_codes(s: pd.Series) -> pd.Series that left-pads each code string with zeros to width 5 using the vectorized s.str.zfill(5) (no Python loop).

The result must meet these requirements:

  • Leave values already 5+ characters unchanged.
  • Keep NaN as NaN .
  • Preserve the original index and name .

Example:

Input:  pd.Series(['7', '42', '12345'])
Output: pd.Series(['00007', '00042', '12345'])

Constraints & Assumptions

  • The Series holds code strings, possibly with NaN for missing codes.
  • The target width is 5 characters.

What a Strong Answer Covers Guidance

  • A vectorized call that pads only the values shorter than 5 characters.
  • How NaN , the index and the Series name come through the call.
  • What happens to elements that are not strings, or that carry a leading sign or whitespace.
  • How the approach changes when the input is numeric.

Follow-up Questions Guidance

  • What if the input Series is numeric ( int64 ) instead of strings?
  • What if the numeric column has missing values, so it arrives as float64 ?
  • How does str.zfill(5) differ from str.pad(5, side='left', fillchar='0') ?