Implementation
This page describes the internal flow of median and the order in which validation and the final median calculation are applied. Unlike Overview, the goal here is not to repeat the function signature, but to show how the result is actually built.
The following snippets are taken from the current src/mespy/stats_utils.py implementation. The only helper involved is _as_float_vector, which defines the numeric input contract before the final delegation to NumPy.
Execution sequence
The implementation follows this sequence:
Converts
xinto a one-dimensional, finitefloat64vector with_as_float_vector("x", x).Rejects empty, multidimensional, or non-finite inputs directly in the shared validator.
Passes the validated vector to
np.median(...).Converts the final result to a Python
float.
Validation and delegation to NumPy
def median(x: ArrayLike) -> float:
values = _as_float_vector("x", x)
return float(np.median(values))
median is the most straightforward function in the whole module.
It contains no alternative branches, optional parameters, or hand-rewritten formulas.
All the robustness of the API is concentrated in the first line:
_as_float_vector(...)requires thatxis a numerical array, one-dimensional, not empty and composed only of finite values.Once
valuesis obtained, the function does not add any other statistical logic: it delegates the calculation of the median tonp.median(values).The final conversion to
floatmakes the public API return type consistent and avoids exposing a NumPy scalar.
Edge cases and practical behavior
This feature does not implement special policies beyond initial validation.
If
xis empty, the failure occurs before callingnp.median(...), so the package avoids warning or silentnan.If
xcontainsNaNor infinities, the rejection always occurs in the common validator, in a manner consistent with all other functions ofstats_utils.If
xhas an even number of elements, the choice on how to combine the two central values is entirely determined by NumPy, becausemespydoes not redefine it.Precisely for this minimal structure, the behavior of
medianis easy to read: common validation at the input, then single delegation to the underlying numerical engine.