Missing readings: NaN spreads, and sentinel codes poison averages
Some systems store "no reading" as a magic number such as -999. Averaging it in gives a nonsense mean. Convert it to NaN first, then use the nan-aware functions. A NaN in a plain mean makes the whole answer NaN, which is NumPy telling you that data is missing.
Aggregations like sum, mean, std and argmax collapse an array along an axis into fewer numbers. A seeded random generator, np.random.default_rng(seed), gives random-looking data that you can repeat exactly.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
For m = np.array([[1.0, 2.0], [3.0, 4.0]]), print the column means and the row sums.
Using default_rng(0), make an array of 5 dice rolls and print how many are above 3.
Replace negative numbers in x = np.array([-2.0, 3.0, -1.0, 5.0]) by 0 using np.where.
For x = np.array([2.0, 4.0, 6.0, 8.0, 10.0]) print the standard deviation the NumPy way and the pandas way (the sample version), rounded to 3 places.
Explain it without notes
What does axis mean in m.sum(axis=0) versus m.sum(axis=1) for a table?
What is the difference between max and argmax, and where is argsort used?
Why use np.random.default_rng(seed) rather than unseeded randomness in a data experiment?
What does np.where(cond, a, b) do and why is it better than a loop?
A sensor file has gaps stored as -999. Why is values.mean() wrong, and what do you do instead?
Why do np.std and pandas Series.std disagree on the same data?
Expected output
naive mean: -313.1666666666667
with NaN: [31.5 nan 29. 30.5 nan 28. ]
mean of NaN data: nan
nanmean: 29.75
missing count: 2
NaN == NaN: False
argmax with NaN: 1 nanargmax: 0
max with NaN: nan nanmax: 31.5