Extract the area code from US phone strings with pandas str.extract
Quick Overview
Write a pandas function that pulls the three-digit area code out of US phone strings with str.extract and returns a named Series. It tests regex capture groups, escaping literal parentheses, the expand argument, index alignment and NaN handling for rows without a match.
Extract the area code from US phone strings with pandas str.extract
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def extract_area_code(s: pd.Series) -> pd.Series` that pulls the 3-digit area code out of US phone strings. In this data the area code is the three digits inside parentheses, as in `(415) 555-1234`.
The function must:
- Use `s.str.extract` with exactly one capture group and return a Series. A one-column DataFrame does not meet the contract.
- Return `NaN` for rows with no match.
- Preserve the original index and name the result `'area_code'`.
For example:
```python
s = pd.Series(['(415) 555-1234', '(212)999-0000', 'no phone'])
extract_area_code(s)
# 0 415
# 1 212
# 2 NaN
# Name: area_code, dtype: object
```
```hint Two kinds of parentheses
The parentheses around the digits in the phone string are literal characters, while parentheses in a regex pattern create a capture group. Decide which ones need escaping, and check what `str.extract` returns for a single group when `expand` is left at its default.
```
### What a Strong Answer Covers
- A pattern that escapes the literal parentheses and captures only the three digits between them.
- Why one capture group with `expand=False` yields a Series, and what the default `expand=True` returns instead.
- How rows without a match, and missing input values, come back as `NaN` with no extra code.
- Keeping the original index and naming the result `area_code`.
- Edge cases such as malformed codes, non-string values and strings holding more than one number.
### Follow-up Questions
- Why use `expand=False` instead of the default?
- How would you also handle numbers without parentheses, such as `415-555-1234`?
- If one string can contain several phone numbers and you need every area code, what would you change?
- How would you return the area code as a nullable integer column, and when would you keep it as a string?
Overview: Write a pandas function that pulls the three-digit area code out of US phone strings with str.extract and returns a named Series. It tests regex capture groups, escaping literal parentheses, the expand argument, index alignment and NaN handling for rows without a match.
Implement def extract_area_code(s: pd.Series) -> pd.Series that pulls the 3-digit area code out of US phone strings. In this data the area code is the three digits inside parentheses, as in (415) 555-1234.
The function must:
Use
s.str.extract
with exactly one capture group and return a Series. A one-column DataFrame does not meet the contract.
Return
NaN
for rows with no match.
Preserve the original index and name the result
'area_code'
.