from tinytab import summarize
result = summarize([3.5, 2.25, 4.0, 1.75])
resultSummaryResult(n=4, mean=2.875, sd=1.051, min=1.75, max=4)
summarize() computes the basic summary statistics of a set of numbers:
SummaryResult(n=4, mean=2.875, sd=1.051, min=1.75, max=4)
summarize() returns a SummaryResult, with one attribute per statistic:
| Attribute | Meaning |
|---|---|
n |
the number of values summarized |
mean |
the arithmetic mean |
sd |
the sample standard deviation (dividing by n - 1) |
min, max |
the smallest and largest values |
The printed form rounds to four significant figures, to keep it readable. The attributes hold the full values.
A result can’t be changed after it’s created. Assigning to an attribute is an error:
--------------------------------------------------------------------------- FrozenInstanceError Traceback (most recent call last) Cell In[3], line 1 ----> 1 result.mean = 0 File <string>:19, in __create_fn__.<locals>.__setattr__(self, name, value) 17 'Could not get source, probably due dynamically evaluated source code.' FrozenInstanceError: cannot assign to field 'mean'
Missing values, written as None or NaN, are left out of the calculation. Since dropping data silently could hide a problem, summarize() issues a warning saying how many were dropped:
/var/folders/s8/bj_jsx3d7jqd2btw7bwm6yx80000gp/T/ipykernel_51131/577297194.py:3: UserWarning: Dropped 2 missing value(s).
summarize([3.5, None, 4.0, math.nan])
SummaryResult(n=2, mean=3.75, sd=0.3536, min=3.5, max=4)
If there are no values left to summarize, summarize() raises an error:
/var/folders/s8/bj_jsx3d7jqd2btw7bwm6yx80000gp/T/ipykernel_51131/1716788409.py:1: UserWarning: Dropped 2 missing value(s).
summarize([None, None])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 1 ----> 1 summarize([None, None]) File ~/py_projects/example-python-package/tinytab/_stats.py:93, in summarize(values) 90 warnings.warn(f"Dropped {n_dropped} missing value(s).", UserWarning, stacklevel=2) 92 if not clean: ---> 93 raise ValueError("Cannot summarize `values`: there are no non-missing values.") 95 n = len(clean) 96 mean = sum(clean) / n ValueError: Cannot summarize `values`: there are no non-missing values.
With only one value, there’s no spread to measure, so the standard deviation is 0: