Aller au contenu

Leçon 17 · Outliers and Impossible Values

Impossible Values and Unit Errors

Catch impossible values with range and cross-field rules, spot mixed units and scale errors, convert them with evidence and keep an audit trail of fixes.

Some values are not just unusual. They cannot be true: a share of a whole above 100%, a discharge before admission, a negative number of people. Unlike statistical outliers, these break known rules, so you can detect them without statistics. A close relative is the unit error, where a correct measurement is stored on the wrong scale.

Types of rules

Rule type Example
Hard limit Relative humidity in your report is between 0% and 100%
Soft limit Adult height outside 130 to 220 cm needs review
Cross-field End date is not before start date
Arithmetic Total equals the sum of its parts
Sequence A cumulative meter reading does not decrease
Domain A status is on the allowed list

Separate hard rules (a violation is an error) from soft ones (a violation is suspicious). Think carefully before you call a rule hard: a meter can be replaced and restart, and a negative invoice can be a credit note.

Write rules as functions

Python Run named rules against every row

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Row 2 has a negative quantity, marked for review because it could be a return. Row 3 breaks two rules. Reporting every failed rule per row helps you find one cause behind several symptoms.

Allow a small tolerance when you compare calculated money or measurements held as floating point numbers. Match the tolerance to the rounding your source uses.

Spot mixed units

Mixed units may show up as two separate clusters, or as values larger or smaller by a familiar factor: 100 (meters and centimeters), 1000 (grams and kilograms) or about 2.2 (pounds and kilograms). A split by source or date can help confirm it.

Python Find a unit problem by source

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Clinic B seems to record meters. Clinic A has one meter-like value too, perhaps a slip. The numbers suggest this, but confirm with the source before you convert.

Convert with evidence

Keep the original value and unit, then create a standard column. Record how you knew the unit. Convert only where the evidence is strong. Leave the rest missing and flagged.

Python Convert and keep the trail

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last record could be kilograms, but nothing says so. Guessing from the size of the number fails exactly where ranges overlap: 150 could be a body weight in pounds or, more rarely, in kilograms.

Put units in names

Name columns weight_kg, duration_seconds or amount_eur. A unit in the name prevents many mistakes later. If one money column mixes currencies, store the currency beside the amount.

Scale and encoding slips

Watch for these too:

  • Amounts stored in cents while another source uses whole currency units.
  • Percentages as 0.4 in one file and 40 in another.
  • Temperatures in Celsius and Fahrenheit.
  • Durations in milliseconds and seconds.
  • Latitude and longitude swapped. Latitude must be between -90 and 90, and longitude between -180 and 180.

A range check does not always catch a swap, because both numbers can fall between -90 and 90. Plot the points or compare them with the expected region.

What to do with impossible values

  1. Look for the true value in the source system.
  2. If you can repair it with certainty, correct it and log it.
  3. Otherwise set the cleaned value to missing and keep the original.
  4. Report counts by rule and by source to the owner.

Common mistake: clipping impossible values to the nearest limit, such as turning an age of 240 into 120. The result passes your rule but is still made up.

Organize rules by confidence

When you have many rules, a small table keeps them honest:

Rule Level Why we believe it Action
Latitude within -90 to 90 Hard Definition of latitude Set to missing, report
End not before start Hard A booking cannot end first Report both fields
Quantity above 500 Soft Rare in past orders Send for review

These entries are examples. The third column matters: a limit based only on "we have never seen it" is a soft rule, because new but valid situations happen.

When two fields disagree

A failed cross-field rule does not say which field is wrong. If an end date comes before a start date, either could contain the error, or day and month could be swapped in one of them. Do not swap them automatically. Look for a third piece of evidence, such as a creation timestamp or the original form. If you find none, flag both.

Calculated columns

If a total disagrees with quantity times price, there may be a legitimate reason that your rule does not know: a discount, tax, shipping or rounding. Before you "fix" totals by recalculating them, find out which value the business treats as the record of what happened. Recalculating can erase real discounts.

Prevent the next one

Report unit and range problems to whoever collects the data. A form that shows the unit next to the box, accepts only a sensible range and asks for confirmation of unusual values prevents many errors at the moment when the person still knows the right answer.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What is a soft rule?

    Soft limits flag unusual cases that may still be correct.
  2. 2. A weight has no unit and the source gives no other clue. What is the safest action?

    Guessing from size fails where ranges overlap.
  3. 3. Why not clip an impossible age to the maximum allowed?

    The true age is unknown, and clipping hides that.

Summary

  • Separate hard rules from soft ones.
  • Report each failed rule per row.
  • Look for factors and source splits that reveal unit mixes.
  • Convert only with evidence, keep originals and name units in columns.

Next: Writing Validation Rules

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.