Choose a quantile method explicitly in Python reports
Two reports can compute different quartiles from the same observations without either sorting the data incorrectly. Record the estimator alongside the result so later runs and other tools can reproduce it.
Run the example
Tested on Linux with Python 3.12.14. No third-party packages are required. In one Bash session, create a disposable directory:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Save this as demo_python_quantiles_explicit_estimation_method.py. Use that filename to avoid shadowing a standard-library module.
from statistics import quantiles, StatisticsError
observations = [10, 20, 30, 40, 50]
for method in ("exclusive", "inclusive"):
cuts = quantiles(observations, n=4, method=method)
print(f"{method}: {cuts}")
try:
quantiles([10], n=4, method="inclusive")
except StatisticsError:
print("single observation: rejected on Python 3.12")Run it:
python3 demo_python_quantiles_explicit_estimation_method.pyOutput:
exclusive: [15.0, 30.0, 45.0]
inclusive: [20.0, 30.0, 40.0]
single observation: rejected on Python 3.12The exclusive estimator treats the observations as a sample from a population that can extend beyond the observed extremes. The inclusive estimator treats the observed minimum and maximum as the population endpoints. The resulting three cut points divide the estimated distribution into four intervals; they need not be observed values.
Input policy and limits
The single-value result above is specific to Python 3.12; later Python versions changed that behavior. Keep the runtime and method in your report metadata. Use a consistent numeric type and establish a policy for nonfinite or missing values before calculating. Small datasets still provide limited evidence about population tails, regardless of how many cut points you request.
Clean up
In the same Bash session, remove the source file and the empty working directory:
rm -- demo_python_quantiles_explicit_estimation_method.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect its contents before deleting anything else.
References
Acknowledgements
This post was created with assistance from AI.