ML Foundations
Compute percentiles when some NumPy observations are missing
Count observed values before using nanpercentile and define an explicit all-missing policy.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Make the missing-data policy explicit
A percentile calculated from incomplete measurements depends on what happens to missing observations. NumPy nanpercentile ignores NaN values, whereas percentile does not apply that missing-value policy. Both use the requested percentile on a 0 to 100 scale. [1, 3]
Dropping missing values changes the sample being summarized. Report the observed count and the total count alongside the estimate, and investigate why values are absent before treating the result as representative.
Compare a filtered sample with nanpercentile
Run python examples/numpy_percentile_missing.py. The fixture contains four values, one of which is NaN. The code uses isnan to select three observed values. For this fixture percentile returns NaN, while nanpercentile returns 30 for the 50th percentile. [1, 2, 3]
The example asserts that nanpercentile matches percentile on the filtered data. This equality applies to this fixture and the matching default method. The input is a small one-dimensional array with finite nonmissing values, not a benchmark or real service data.
Executed with Python 3.12.14 and NumPy 2.3.5; output and checksum are stored in private evidence.
import numpy as np
samples = np.array([10.0, np.nan, 30.0, 50.0])
observed = samples[~np.isnan(samples)]
print('input count:', samples.size)
print('observed count:', observed.size)
print('percentile with NaN is NaN:', bool(np.isnan(np.percentile(samples, 50))))
print('nanpercentile p50:', float(np.nanpercentile(samples, 50)))
print('filtered percentile p50:', float(np.percentile(observed, 50)))
assert np.nanpercentile(samples, 50) == np.percentile(observed, 50)
empty = np.array([np.nan, np.nan])
count = int(np.count_nonzero(~np.isnan(empty)))
print('all-missing observed count:', count)
print('all-missing policy:', 'no estimate' if count == 0 else 'estimate')
assert count == 0
Recorded output
input count: 4 observed count: 3 percentile with NaN is NaN: True nanpercentile p50: 30.0 filtered percentile p50: 30.0 all-missing observed count: 0 all-missing policy: no estimate
Guard an all-missing slice
If no observations remain after excluding NaN, the example reports no estimate instead of calling the percentile routine. This makes the missing-data decision visible and avoids presenting a numeric value for an empty sample. Count separately for each slice when working along an axis. [1, 2]
This guard detects NaNs, not infinities or invalid units. Validate finite values and units separately when your application requires them. Also record the percentile method and sampling window; ignoring NaNs does not correct bias from nonrandom missing observations.