Complete worked example
python
import numpy as np
def inspect_example(values):
data = np.asarray(values, dtype=np.float64)
if data.ndim != 1:
raise ValueError("expected a vector")
valid = np.isfinite(data)
selected = data[valid]
return {"validCount": int(selected.size),
"excludedCount": int(data.size - selected.size),
"mean": float(selected.mean()) if selected.size else None}- Reasoning
- The worked function uses isfinite to build one mask, selects the corresponding values, and computes a mean only when the selected count is nonzero. Explicit int and float conversions produce ordinary Python scalars at the report boundary. The examples include None, a string that converts to infinity, negative values and an empty vector so that each excluded-value decision is visible. The numeric conversion policy is narrow and documented here. It is not a general-purpose parser for currency labels, locale-specific decimals or arbitrary source text. Those require separate validation before entering a numerical array. Errors in source parsing must not be hidden by treating every unrecognized token as a legitimate missing observation.