Convert Percent Strings to Fractions in pandas

Quick Overview

Write a pandas function that turns percent strings such as 45% or 3.5% into fractional float64 values while keeping the index and name. It tests vectorized string cleaning, pd.to_numeric, dtype control, and how to handle malformed entries, mixed formats and float comparisons in tests.

Convert Percent Strings to Fractions in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def percent_to_fraction(s: pd.Series) -> pd.Series` that converts an object Series of percent strings (e.g. `'45%'`, `'3.5%'`, `'100%'`) into fractional floats (`0.45`, `0.035`, `1.0`). The function must: - Remove the trailing `'%'`, convert with `pd.to_numeric`, and divide by 100. - Return dtype `float64`. - Preserve the original `index` and `name`. Example: ```python s = pd.Series(['45%', '3.5%', '100%'], name='rate') percent_to_fraction(s) # 0 0.450 # 1 0.035 # 2 1.000 # Name: rate, dtype: float64 ``` ```hint Follow the dtype The requirements already list the steps. For each step, check what dtype it produces and whether the index and name come through it. ``` ### What a Strong Answer Covers - Removes the trailing percent sign with a vectorized string operation and parses the rest with `pd.to_numeric`. - Scales by 100 with true division and returns `float64`, including when every input is a whole number, and knows when an explicit cast is still needed. - Keeps the input index and name. - Says what happens on missing or malformed entries and on values that have no percent sign. ### Follow-up Questions - How would you handle entries that fail to parse, such as `'n/a'`, without raising? - The column mixes `'45%'` and `'0.45'` because two upstream systems wrote it. How would you detect and fix that? - Why can an exact equality check on the parsed fractions fail in a unit test, and what would you use instead?

Overview: Write a pandas function that turns percent strings such as 45% or 3.5% into fractional float64 values while keeping the index and name. It tests vectorized string cleaning, pd.to_numeric, dtype control, and how to handle malformed entries, mixed formats and float comparisons in tests.

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

Implement def percent_to_fraction(s: pd.Series) -> pd.Series that converts an object Series of percent strings (e.g. '45%', '3.5%', '100%') into fractional floats (0.45, 0.035, 1.0).

The function must:

  • Remove the trailing '%' , convert with pd.to_numeric , and divide by 100.
  • Return dtype float64 .
  • Preserve the original index and name .

Example:

s = pd.Series(['45%', '3.5%', '100%'], name='rate')
percent_to_fraction(s)
# 0    0.450
# 1    0.035
# 2    1.000
# Name: rate, dtype: float64

What a Strong Answer Covers Guidance

  • Removes the trailing percent sign with a vectorized string operation and parses the rest with pd.to_numeric .
  • Scales by 100 with true division and returns float64 , including when every input is a whole number, and knows when an explicit cast is still needed.
  • Keeps the input index and name.
  • Says what happens on missing or malformed entries and on values that have no percent sign.

Follow-up Questions Guidance

  • How would you handle entries that fail to parse, such as 'n/a' , without raising?
  • The column mixes '45%' and '0.45' because two upstream systems wrote it. How would you detect and fix that?
  • Why can an exact equality check on the parsed fractions fail in a unit test, and what would you use instead?