Pull In the Extremes

Data with PythonHard+10 XP+10

Pull In the Extremes

Deleting an outlier loses the whole row. A gentler fix is capping: any value past a fence is pulled back to sit exactly on that fence.

Write cap_outliers(values). values is a list of numbers, where None marks a missing reading.

  1. Work out the fences with the 1.5 × IQR rule, using pandas quantile(0.25) and quantile(0.75) on the known values only.
  2. Replace every value below the low fence with the low fence, and every value above the high fence with the high fence. Values between the fences stay as they are, and a value exactly on a fence stays too.
  3. Leave each None as None, in its place.

Return a list of the same length as values, with every number rounded to 2 decimal places. There is always at least one known value.

Examples

values = [12, 14, 15, 15, 16, 18, 19, 21, 95]
returns [12, 14, 15, 15, 16, 18, 19, 21, 25.0]
values = [20, None, 22, 2, 21, 23, 60, 22]
returns [20, None, 22, 17.5, 21, 23, 25.5, 22]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run