Parse US Currency Strings to Floats in pandas

Quick Overview

Write a pandas function that turns US dollar amount strings with thousands separators into float64 values. It tests vectorized string cleaning with a regular expression, keeping the index and name, and how to handle malformed entries, other locales and the precision limits of floats for money.

Parse US Currency Strings to Floats in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `parse_currency(s: pd.Series) -> pd.Series`, converting an object Series of US currency strings (e.g. `'$1,200'`, `'$1,234.50'`) to floats by stripping `'$'` and `','`. The function must: - Return dtype `float64`. - Preserve the input's `index` and `name`. Example: ```python s = pd.Series(['$1,200', '$45', '$1,234.50']) parse_currency(s) # 0 1200.0 # 1 45.0 # 2 1234.5 # dtype: float64 ``` ```hint One pass Both formatting characters can be removed in a single vectorized string operation. Check which of them has a special meaning in a regular expression. ``` ### Constraints & Assumptions - Every entry is a well-formed US amount: a leading `$`, digits with optional comma thousands separators, and an optional decimal part. - Missing and malformed entries are covered in the follow-ups, not in the base task. ### What a Strong Answer Covers - Removes both formatting characters with a vectorized operation and explains why the pattern leaves digits and the decimal point alone. - Returns `float64` and keeps the input index and name. - Says what the function does on missing, malformed or negative entries. - Discusses whether a float is the right type for money. ### Follow-up Questions - How would you handle missing or malformed entries without raising? - How would you extend this to parse other currencies or locales? - The amounts feed an accounting report that has to reconcile to the cent. Would you still return floats?

Overview: Write a pandas function that turns US dollar amount strings with thousands separators into float64 values. It tests vectorized string cleaning with a regular expression, keeping the index and name, and how to handle malformed entries, other locales and the precision limits of floats for money.

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

Implement parse_currency(s: pd.Series) -> pd.Series, converting an object Series of US currency strings (e.g. '$1,200', '$1,234.50') to floats by stripping '$' and ','.

The function must:

  • Return dtype float64 .
  • Preserve the input's index and name .

Example:

s = pd.Series(['$1,200', '$45', '$1,234.50'])
parse_currency(s)
# 0    1200.0
# 1      45.0
# 2    1234.5
# dtype: float64

Constraints & Assumptions

  • Every entry is a well-formed US amount: a leading $ , digits with optional comma thousands separators, and an optional decimal part.
  • Missing and malformed entries are covered in the follow-ups, not in the base task.

What a Strong Answer Covers Guidance

  • Removes both formatting characters with a vectorized operation and explains why the pattern leaves digits and the decimal point alone.
  • Returns float64 and keeps the input index and name.
  • Says what the function does on missing, malformed or negative entries.
  • Discusses whether a float is the right type for money.

Follow-up Questions Guidance

  • How would you handle missing or malformed entries without raising?
  • How would you extend this to parse other currencies or locales?
  • The amounts feed an accounting report that has to reconcile to the cent. Would you still return floats?