Tables that tell the truth:
cleaning without rewriting the geology

Session 5 · Fri Oct 9 · Book sections 2.3–2.4

🧮

Today’s question

Cleaning changes your data before any model ever sees it.

Which of your table’s oddities are damage — and which are the science?

This lecture in the literature

📖 The playbook: whether missing data can be ignored depends on why they are missing — the origin of MCAR / MAR / MNAR.

Rubin, D. B. (1976). Inference and missing data. Biometrika, 63, 581–592.

A spreadsheet’s excluded rows propped up a global austerity argument — reanalysis of the same table reversed the headline result.

Herndon, T., Ash, M., & Pollin, R. (2014). Does high public debt consistently stifle economic growth? A critique of Reinhart and Rogoff. Cambridge Journal of Economics, 38, 257–279.

Doing it right: substituting values at the detection limit fabricates data — model the censoring instead.

Helsel, D. R. (2006). Fabricating data: How substituting values for nondetects can ruin results, and what can be done about it. Chemosphere, 65, 2434–2439.

Rows dropped from a table have moved national policy. Cleaning decisions are scientific claims.

One library, every table

  • A global earthquake catalog: 1,785 events, M ≥ 6.0, depths 0–685 km, with timestamps and place names
  • The same verbs every time: read, describe, filter, transform, group, join, map
  • Datetime-aware: select by time window, resample by day, roll a moving average

Learn the verbs, not the spellings — your coding agent knows the syntax; you must know what to ask for.

Today’s table: a rock survey with planted truth

10,000 rock samples — 7 major-element oxides, density, magnetic susceptibility, lithology

985 andesites — against 5,525 granites: a 5.6 : 1 class imbalance

Synthetic whole-rock geochemistry (mlgeo_synth): the generator plants the correlations and the pathologies, so every cleaning decision is graded against known truth.

Imbalance is the norm in geoscience labels — remember 985 when a classifier reports 90% accuracy in Chapter 3.

A zero is not one thing

Column Zeros in the table Verdict
MgO 1,434 real — evolved granites have fractionated their magnesium away
K2O 824 real — same logic, other rock types
density 15 impossible — no rock has zero density: an instrument’s failure code

The check: is zero inside the physically possible range of this variable?

“Replace all zeros with missing” would delete 1,432 silica-rich granites — rewriting the geology, not cleaning it.

Censored: present, plausible, and wrong

12% of samples sit exactly at the 5 mm floor — yet a missing-data check reports zero problems. Censored data passes every missingness test.

What the floor does to the science

12.44 mm/yr trend fit on the true, uncensored values

11.59 mm/yr trend fit on what the sensor reported

Every low excursion pulled up to the floor: the early mean is too high, the fitted trend too low — biased in a predictable direction. Dropping the censored samples deletes exactly the low values: worse.

Model the censoring, or report the limit in the data card — never substitute and move on.

Why is it missing? The gap can be the data

  • The stream gauge drowns in the flood it was built to measure
  • The GNSS antenna goes silent under the storm’s snow load
  • The field crew skips the location when the road washes out

Missing completely at random (MCAR) — safe to drop. Missing because of the value (MNAR) — dropping deletes your extremes.

Ask what process made the gap. If the answer involves the measurement, the missingness belongs in your data card as data.

The dtype trap

  • Cleaning density: replace the impossible zeros with a missing marker — correct
  • pandas silently upcast the column: float64 → object (numbers stored as generic objects)
  • Every later “numeric columns only” step — correlations, summaries — now drops density without a warning

A column can be numeric in content and non-numeric in dtype. Check dtypes after every cleaning step, not just the first.

Four pathologies, four fingerprints

Pathology Fingerprint Geoscience example Response
Sentinel value heap at an impossible value density = 0 g/cm³ replace only the impossible ones
Censored value heap at a plausible round value 5 mm detection floor model it, or report the limit
Informative gap gaps track events gauge drowned in the flood treat the gap as data
Dtype drift numeric content, non-numeric dtype density after cleaning check dtypes every step

Same discipline throughout: diagnose the mechanism first; clean second; write the decision down.

Now run it yourself — open 2.4

  1. pixi run jupyter lab2.4_dataframes_prep.ipynb
  2. Run the cleaning: count the zeros, replace only the impossible ones, catch the dtype flip and fix it
  3. Reproduce the censoring bias: 11.59 vs 12.44 mm/yr — then try dropping the censored samples and watch it get worse
  4. Exercise Q3: what accuracy does “always predict granite” achieve — and what does that imply for evaluating any classifier on this table?

Monday: sampling, resampling & irregular data (2.5–2.6) · HW1 due Mon Oct 12