Write a pandas function that returns a lowercased copy of a string Series while keeping NaN values, the original index and the name intact. It tests vectorized string methods, how missing values flow through them, and writing cleaning code that never mutates its input.
Implement `def to_lower(s: pd.Series) -> pd.Series` that returns a new Series with every string value lowercased using `s.str.lower()`.
Requirements:
- Do not mutate the input.
- Leave `NaN` values as `NaN`.
- Preserve the original `index` and `name`.
Example:
```python
to_lower(pd.Series(['ALICE', 'Bob', 'cAROL']))
# returns pd.Series(['alice', 'bob', 'carol'])
```
```hint What the accessor gives back
Check whether `s.str.lower()` changes `s` in place or builds a new object, and what it does with entries that are not strings.
```
### What a Strong Answer Covers
- Which pandas method does the work, and why a vectorized method fits this task
- Whether the input is modified, and whether an explicit copy is needed
- How missing values pass through, and what a per-element Python call would do with them
- How the result keeps the original index and name
- What happens with non-string values or a non-text dtype
### Follow-up Questions
- The Series holds strings, integers and missing values together. What does `s.str.lower()` return for each, and how would you keep the integers unchanged?
- When would you use `str.casefold()` instead of `str.lower()`?
- What changes if the Series uses the `string` dtype instead of `object`?
- How would you lowercase every text column of a DataFrame without modifying the original DataFrame?
Overview: Write a pandas function that returns a lowercased copy of a string Series while keeping NaN values, the original index and the name intact. It tests vectorized string methods, how missing values flow through them, and writing cleaning code that never mutates its input.
Which pandas method does the work, and why a vectorized method fits this task
Whether the input is modified, and whether an explicit copy is needed
How missing values pass through, and what a per-element Python call would do with them
How the result keeps the original index and name
What happens with non-string values or a non-text dtype
Follow-up Questions Guidance
The Series holds strings, integers and missing values together. What does
s.str.lower()
return for each, and how would you keep the integers unchanged?
When would you use
str.casefold()
instead of
str.lower()
?
What changes if the Series uses the
string
dtype instead of
object
?
How would you lowercase every text column of a DataFrame without modifying the original DataFrame?