bhylu.com

Advertisements

NumPy Missing Values

NaN can represent an unavailable floating measurement, while zero can be a real recorded quantity. A mean must follow an explicit policy for missing observations.

How it works

np.mean propagates NaN in this fixture; nanmean omits it. Omitting a missing value changes the number of observations and can bias an analysis if missingness has a pattern. Count valid values alongside the result and define what happens when every value is missing. Do not replace unknown measurements with zero simply to make an operation return a finite number.

Run the code

Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.

For local work, use a separate virtual environment and run python missing-data.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

missing-data.py

import numpy as np

values = np.array([12., np.nan, 24.])
valid = np.isfinite(values)
print(valid.tolist())
print(np.isnan(values.mean()))
print(float(np.nanmean(values)), int(valid.sum()))

Expected result

[True, False, True]
True
18.0 2

The accepted average is eighteen from two finite observations, not three. The validity count is part of the result’s meaning. Infinity is also rejected by isfinite, so decide whether that is the intended input policy.

Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.