Mask Outliers Above a Threshold and Impute the Mean in pandas
Quick Overview
Write a pandas function that replaces values above a threshold with NaN and then fills every missing value with the mean of what remains. It tests the order of operations in imputation, NaN handling in comparisons and fillna, dtype care, and the case where nothing valid is left.
Mask Outliers Above a Threshold and Impute the Mean in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `mask_and_impute(s, threshold)`. Mask values strictly greater than `threshold` to `NaN`, then fill every `NaN` (masked or already missing) with the mean of the remaining valid values. Return a new `float64` Series.
The function must follow these rules:
- Values where `s > threshold` become `NaN`.
- Compute the mean once on the post-mask valid values (`skipna=True`), so every imputed cell equals that single mean.
- Fill all `NaN`s with that mean via `fillna`.
- Preserve the original index and order. Do not mutate `s`.
For example:
```python
s = pd.Series([10, 999, 30, None, 20])
mask_and_impute(s, threshold=100)
# mean of in-range valid values [10, 30, 20] = 20.0
# -> 0 10.0
# 1 20.0 # 999 masked, filled
# 2 30.0
# 3 20.0 # pre-existing NaN, filled
# 4 20.0
# dtype: float64
```
```hint Watch the order
Ask which values should be allowed to influence the fill value, then order the steps so that only those values do.
```
### Constraints & Assumptions
- `s` is numeric (integer or float) and may already contain `NaN`.
- `threshold` is a single number, and only values above it are treated as outliers.
### What a Strong Answer Covers
- The order of masking and averaging, and why it changes the fill value
- The strict comparison, and what happens to values that were already missing
- A single scalar fill, a `float64` result, the original index, and an untouched input
- What the function returns when no valid value remains after masking
### Follow-up Questions
- What if all values exceed `threshold`, so no valid values remain?
- How would you extend this to impute per-group means in a DataFrame?
- Mean imputation shrinks the spread of a column. When would you use the median or add an indicator column instead?
Overview: Write a pandas function that replaces values above a threshold with NaN and then fills every missing value with the mean of what remains. It tests the order of operations in imputation, NaN handling in comparisons and fillna, dtype care, and the case where nothing valid is left.
Implement mask_and_impute(s, threshold). Mask values strictly greater than threshold to NaN, then fill every NaN (masked or already missing) with the mean of the remaining valid values. Return a new float64 Series.
The function must follow these rules:
Values where
s > threshold
become
NaN
.
Compute the mean once on the post-mask valid values (
skipna=True
), so every imputed cell equals that single mean.
Fill all
NaN
s with that mean via
fillna
.
Preserve the original index and order. Do not mutate
s
.