Getting and cleaning data · lesson 6 of 11
Parts in this lesson
As.isna().groupby(key).mean()the share missing in each group
Bs.interpolate()fill gaps from the values either side
Cs.isna()3.8: a flag to keep beside a fill

Missing data in depth

Last updated

Lesson 3.8 found missing values and dropped or filled them. Which is right depends on why the values are missing, and that changes what the answer means. In this lesson you'll simulate the three ways data goes missing, see what each does to an average, look for patterns in what's missing, and see what filling a typhoon week with the mean does to a month's total.

By the end of this lesson you can

  • Name the three kinds of missingness and say how each biases an average
  • Look for patterns in missing values by group
  • Choose between dropping, filling and flagging, and say why

Three reasons data goes missing

Statisticians name three kinds of missing data (after Rubin, 1976). Step through them:

Three patterns of missing values. Three weeks of sales, nothing missing. Weekends (shaded) sell more.444741454861584639454347636042454840465962kilos soldMonTueWedThuFriSatSuntrue mean: 49.0
1 of 4 Three weeks of sales, nothing missing. Weekends (shaded) sell more.
  • Missing completely at random: why a value is missing has nothing to do with anything in the data. Dropping those rows costs you data but doesn't bias the answer.
  • Missing at random: missingness depends on something you can see, like the day of the week. The answer is biased, but you can correct it using what you see.
  • Missing not at random: missingness depends on the missing value itself, like slow days not being written down. The answer is biased, and the data alone can't show it.

Now the same three on 91 days of simulated sales. Run the cells.

missing.ipynb

The true mean is 48.5 kg. With 15 days lost completely at random, the mean is 48.4: hardly changed. With weekend days missing, it's 46.3, too low, because weekends sell more. With slow days missing, it's 50.4, too high.

The third cell finds the pattern: the share missing by day of the week is 0 on weekdays, and 0.69 and 0.62 on Saturday and Sunday. Always look at where the gaps are, by store, day, month and any other column, before deciding anything.

Because that pattern is visible, it can be corrected: the last cell takes the weekday and weekend means separately and weights them by 5 days and 2, giving 48.7, close to the truth. Slow days missing can't be fixed this way. Only knowing why the values are missing can tell you which kind you have; ask the people who collected the data.

Wrong way: a typhoon week filled with the mean

A typhoon closes a store for three days in July. Nobody records sales. Run the cells.

typhoon.ipynb

Sales fell to 18 kg on the 25th, are missing for three days, and recover from 12 kg on the 29th. Filling the gap with the mean puts 45.8 kg on each of the three days: an ordinary day's sales, in the middle of a typhoon. The month's total jumps from 779 kg (on the 17 days known) to 916.5 kg: 137.5 kg of rice that was never sold.

These values are missing not at random: they're missing because of what was happening. The mean of normal days says nothing about them.

Interpolating draws a straight line between the known days on each side: 16.5, 15 and 13.5 kg, a total of 824 kg. That's believable, but it's still a guess. The last cell keeps the estimate and adds an estimated column, so every later table and chart can show which values were invented.

Drop, fill or flag

There's no right answer for every gap, but there's a right way to decide:

choicegood whencost
Drop rowsfew, and missing completely at randomless data; biased if not random
Fill with a fixed valuethe value is known, like 0 for a day the store was closedwrong if you're not sure
Fill from neighbours (interpolate, ffill)values change slowly, like stock or temperatureinvents values; hides real drops
Fill with the mean or medianrarely: values missing completely at random, and a tool can't take gapsmakes every gap look ordinary, and the spread too small
Flag and leaveyou don't know why they're missingevery later step must handle the gaps

Whatever you choose: count the gaps, say what you did and why in a note, and keep a flag for every filled value. Report results both ways when it changes the answer.

Check what you learned

A survey’s income question is left blank most often by people with the highest incomes. What kind of missing is that?
A store was closed for two days, and you know it. What should those days’ sales be?
You interpolate three missing days. What else should you do?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis