Overview
A single missing value turns a NumPy average into nan. Spot the gaps with np.isnan() and average around them with np.nanmean().
- np.nan marks a missing value
- nan == nan is False
- np.isnan() and np.nanmean()
Short Course
np.nan marks a missing number, and it spreads: any sum or mean that
includes it becomes nan. Trap: nan == nan is False, so test for it with
np.isnan().
r = np.array([20.0, np.nan, 22.0])
r.mean() # nan
np.nanmean(r) # 21.0
np.isnan(r).sum() # 1 missing value
Task
Write average_reading(readings) that returns the mean of the readings,
ignoring missing ones, rounded to 1 decimal, for example
average_reading([20, np.nan, 23]) gives 21.5.
Test Suites
Pending
Next
Output