Extract year, month and day from date strings with a pandas named-group regex
Quick Overview
Parse YYYY-MM-DD strings in a pandas Series into separate year, month and day columns with one regex extraction that uses named groups. It tests str.extract, how unmatched rows and missing values come out, keeping strings and the index intact, and what a date regex does not validate.
Extract year, month and day from date strings with a pandas named-group regex
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `extract_date_parts(s: pd.Series) -> pd.DataFrame`, which parses `'YYYY-MM-DD'` strings into the columns `'year'`, `'month'` and `'day'` using a single `s.str.extract` call with named groups (keep the default `expand=True`).
The function must:
- Use one regex whose named groups match a four-digit year, a two-digit month and a two-digit day, separated by hyphens.
- Return columns named exactly `'year'`, `'month'` and `'day'`, holding the matched text as strings (object dtype on pandas 2.x).
- Put `NaN` in every column for a row that does not match.
- Preserve the index of the input Series.
Example:
```python
s = pd.Series(['2026-01-15', 'bad', '1999-12-31'])
extract_date_parts(s)
# year month day
# 0 2026 01 15
# 1 NaN NaN NaN
# 2 1999 12 31
```
```hint Keep the parts as text
Do not convert the parts to integers. That would drop the leading zero in `'01'`, and a row that fails to match would turn its column into floats.
```
### Constraints & Assumptions
- Values are expected to be `'YYYY-MM-DD'` strings, but the Series can also hold malformed strings and missing values.
- Match only the shape (digits and hyphens). Checking that the month and day are real calendar values is out of scope for the main task.
### What a Strong Answer Covers
- A single vectorized `str.extract` call whose group names become the column labels
- Keeping the parts as strings and keeping the original index
- What happens to rows that do not match, including missing values
- What the pattern does not validate, such as extra characters around the date or impossible months and days
### Follow-up Questions
- How would you convert the parsed parts into a single `datetime64` column?
- Why use named groups instead of positional groups here?
- How would you reject strings with extra characters or impossible dates such as `'2026-13-45'`?
Overview: Parse YYYY-MM-DD strings in a pandas Series into separate year, month and day columns with one regex extraction that uses named groups. It tests str.extract, how unmatched rows and missing values come out, keeping strings and the index intact, and what a date regex does not validate.
Implement extract_date_parts(s: pd.Series) -> pd.DataFrame, which parses 'YYYY-MM-DD' strings into the columns 'year', 'month' and 'day' using a single s.str.extract call with named groups (keep the default expand=True).
The function must:
Use one regex whose named groups match a four-digit year, a two-digit month and a two-digit day, separated by hyphens.
Return columns named exactly
'year'
,
'month'
and
'day'
, holding the matched text as strings (object dtype on pandas 2.x).
Put
NaN
in every column for a row that does not match.
Preserve the index of the input Series.
Example:
s = pd.Series(['2026-01-15', 'bad', '1999-12-31'])
extract_date_parts(s)
# year month day
# 0 2026 01 15
# 1 NaN NaN NaN
# 2 1999 12 31
Constraints & Assumptions
Values are expected to be
'YYYY-MM-DD'
strings, but the Series can also hold malformed strings and missing values.
Match only the shape (digits and hyphens). Checking that the month and day are real calendar values is out of scope for the main task.
What a Strong Answer Covers Guidance
A single vectorized
str.extract
call whose group names become the column labels
Keeping the parts as strings and keeping the original index
What happens to rows that do not match, including missing values
What the pattern does not validate, such as extra characters around the date or impossible months and days
Follow-up Questions Guidance
How would you convert the parsed parts into a single
datetime64
column?
Why use named groups instead of positional groups here?
How would you reject strings with extra characters or impossible dates such as
'2026-13-45'
?