Write a pandas function that turns percent strings such as 45% or 3.5% into fractional float64 values while keeping the index and name. It tests vectorized string cleaning, pd.to_numeric, dtype control, and how to handle malformed entries, mixed formats and float comparisons in tests.
Implement `def percent_to_fraction(s: pd.Series) -> pd.Series` that converts an object Series of percent strings (e.g. `'45%'`, `'3.5%'`, `'100%'`) into fractional floats (`0.45`, `0.035`, `1.0`).
The function must:
- Remove the trailing `'%'`, convert with `pd.to_numeric`, and divide by 100.
- Return dtype `float64`.
- Preserve the original `index` and `name`.
Example:
```python
s = pd.Series(['45%', '3.5%', '100%'], name='rate')
percent_to_fraction(s)
# 0 0.450
# 1 0.035
# 2 1.000
# Name: rate, dtype: float64
```
```hint Follow the dtype
The requirements already list the steps. For each step, check what dtype it produces and whether the index and name come through it.
```
### What a Strong Answer Covers
- Removes the trailing percent sign with a vectorized string operation and parses the rest with `pd.to_numeric`.
- Scales by 100 with true division and returns `float64`, including when every input is a whole number, and knows when an explicit cast is still needed.
- Keeps the input index and name.
- Says what happens on missing or malformed entries and on values that have no percent sign.
### Follow-up Questions
- How would you handle entries that fail to parse, such as `'n/a'`, without raising?
- The column mixes `'45%'` and `'0.45'` because two upstream systems wrote it. How would you detect and fix that?
- Why can an exact equality check on the parsed fractions fail in a unit test, and what would you use instead?
Overview: Write a pandas function that turns percent strings such as 45% or 3.5% into fractional float64 values while keeping the index and name. It tests vectorized string cleaning, pd.to_numeric, dtype control, and how to handle malformed entries, mixed formats and float comparisons in tests.
Implement def percent_to_fraction(s: pd.Series) -> pd.Series that converts an object Series of percent strings (e.g. '45%', '3.5%', '100%') into fractional floats (0.45, 0.035, 1.0).
The function must:
Remove the trailing
'%'
, convert with
pd.to_numeric
, and divide by 100.