Missing Values
Detect Missing
Identify which positions in a list of sensor readings are missing (None)
and count how many are absent. The trace shows flags growing one boolean
at a time and count incrementing only on missing values.
By hand
Iterate over values. For each element test v is None to get a boolean,
append it to flags, and increment count when the test is True.
naive.py
Replay: real traced execution (multi-file project)
values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
flags = []
count = 0
for v in values:
missing = v is None
flags.append(missing)
if missing:
count = count + 1
print('RESULT:', (count, flags))
values ← [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
1values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]2flags = []values this step[3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]valuesflags ← []
1values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]2flags = []3count = 0values this step[]flagscount ← 0
2flags = []3count = 04for v in values:values this step0countv ← None, missing ← True, flags ← [False, True], count ← 1
pass 1 of 33count = 04for v in values:5 missing = v is None6 flags.append(missing)7 if missing:8 count = count + 19print('RESULT:', (count, flags))values this step3.1 → NonevFalse → Truemissing[False] → [False, True]flags0 → 1countAll 3 passes — pass 1 is the card above pass vmissingflagscount1 3.1 → None False → True [False] → [False, True] 0 → 1 2 7.2 → None False → True [False, True, False] → [False, True, False, True] 1 → 2 3 5.0 → None False → True [False, True, False, True, False] → [False, True, False, True, False, True] 2 → 3 v ← 8.4, missing ← False, flags ← [False, True, False, True, False, True, False]
pass 1 of 23count = 04for v in values:5 missing = v is None6 flags.append(missing)7 if missing:8 count = count + 1values this stepNone → 8.4vTrue → Falsemissing[False, True, False, True, False, True] → [False, True, False, True, False, True, False]flagsv ← 2.9, flags ← [False, True, False, True, False, True, False, False]
pass 2 of 23count = 04for v in values:5 missing = v is None6 flags.append(missing)7 if missing:8 count = count + 1values this step8.4 → 2.9v[False, True, False, True, False, True, False] → [False, True, False, True, False, True, False, False]flagsfor v in values:
3count = 04for v in values:5 missing = v is Nonestdout ← RESULT: (3, [False, True, False, True, False, True, False, False])
8 count = count + 19print('RESULT:', (count, flags))values this stepRESULT: (3, [False, True, False, True, False, True, False, False])stdout
With pandas
Load the values into a pd.Series. isna() returns a boolean Series
marking each missing position; .tolist() converts it to a plain list and
.sum() counts the True entries.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
s = pd.Series(values, dtype=float)
flags = s.isna().tolist()
count = int(s.isna().sum())
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('values:', s.tolist())
print('RESULT:', (count, flags))
index: [0, 1, 2, 3, 4, 5, 6, 7]
dtype: float64
values: [3.1, nan, 7.2, nan, 5.0, nan, 8.4, 2.9]
RESULT: (3, [False, True, False, True, False, True, False, False])
Implementation notes
v is Noneis preferred overv == Nonebecause==can be overridden by custom__eq__;istests object identity and is always safe forNonechecks.pd.Series(values, dtype=float)makes the numeric dtype explicit. pandas already infersfloat64(replacingNonewithNaN) for a numeric list like this one, so thedtype=floatargument is defensive rather than corrective — it documents intent and guards against edge cases where inference might chooseobject(e.g. a list containing mixed types).int(s.isna().sum())casts the NumPy scalar to a plain Pythonintso the RESULT tuple round-trips throughast.literal_evalunchanged.