Recode single-letter status codes in a pandas Series to their full labels with Series.map and a dictionary, keeping the input's index and name. It tests how map looks values up, what happens to codes missing from the mapping, and how map differs from replace.
Implement `def recode_status(s: pd.Series) -> pd.Series`. Given a Series of status codes (each one of `'A'`, `'I'`, `'P'`), return a new Series with each code recoded to its full label.
Requirements:
- Use `s.map({'A':'Active','I':'Inactive','P':'Pending'})` with that exact mapping.
- Preserve the input's `name` and `index`.
- The result has `object` dtype. Do not mutate the input.
Example:
```python
s = pd.Series(['A', 'P', 'I', 'A'], name='status')
recode_status(s)
# 0 Active
# 1 Pending
# 2 Inactive
# 3 Active
# Name: status, dtype: object
```
```hint How map treats a dict
Look at what `Series.map` returns when you pass it a dict: the index, the name, and what happens to a value that is not one of the keys.
```
### Constraints & Assumptions
- Every value in `s` is one of `'A'`, `'I'` or `'P'`.
### What a Strong Answer Covers
- Calling `Series.map` with the exact dictionary and returning its result.
- Confirming that the result is a new Series with the same index and name, and that `s` is unchanged.
- What `map` does with a value that is not a key in the dictionary.
- The dtype of the returned Series.
### Follow-up Questions
- A new code such as `'X'` starts appearing in the data. What does your function return for it, and how would you make it fail loudly instead?
- How does `s.map(mapping)` differ from `s.replace(mapping)`?
- When would you store the recoded column as a categorical instead of plain strings?
Overview: Recode single-letter status codes in a pandas Series to their full labels with Series.map and a dictionary, keeping the input's index and name. It tests how map looks values up, what happens to codes missing from the mapping, and how map differs from replace.
Implement def recode_status(s: pd.Series) -> pd.Series. Given a Series of status codes (each one of 'A', 'I', 'P'), return a new Series with each code recoded to its full label.
Requirements:
Use
s.map({'A':'Active','I':'Inactive','P':'Pending'})
with that exact mapping.
Preserve the input's
name
and
index
.
The result has
object
dtype. Do not mutate the input.
Example:
s = pd.Series(['A', 'P', 'I', 'A'], name='status')
recode_status(s)
# 0 Active
# 1 Pending
# 2 Inactive
# 3 Active
# Name: status, dtype: object
Constraints & Assumptions
Every value in
s
is one of
'A'
,
'I'
or
'P'
.
What a Strong Answer Covers Guidance
Calling
Series.map
with the exact dictionary and returning its result.
Confirming that the result is a new Series with the same index and name, and that
s
is unchanged.
What
map
does with a value that is not a key in the dictionary.
The dtype of the returned Series.
Follow-up Questions Guidance
A new code such as
'X'
starts appearing in the data. What does your function return for it, and how would you make it fail loudly instead?
How does
s.map(mapping)
differ from
s.replace(mapping)
?
When would you store the recoded column as a categorical instead of plain strings?