[Python-ideas] Running average and stdev in the statistics module?

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon May 6 20:08:41 EDT 2019


On Sun, 5 May 2019 at 11:08, Luca Baldini <luca.baldini at pi.infn.it> wrote:
>
> Hi here,
> I wonder if the idea of adding to the statistics module a class to
> calculate the running statistics (average and standard deviation) of a
> generic input data stream has ever come up in the past.

There was discussion around this when the PEP was written. I think
this is what is alluded to in the PEP with
"""
There is considerable interest in including one-pass functions that
can calculate multiple statistics from data in iterator form, without
having to convert to a list. The experimental stats package on PyPI
includes co-routine versions of statistics functions. Including these
will be deferred to 3.5.
"""
https://www.python.org/dev/peps/pep-0450/#future-work

At the time I believe there was brief discussion about what the API
should look like but it was deferred for future work.

For the sakes of argument how about:

from statistics import RunningStats, RunningMean, RunningMax

stats = RunningStats({'mean':RunningMean, 'max':RunningMax})
stats.push_many([1, 5, 2, 4])
print(stats.running_stats()) # {'mean': 3, 'max': 5}

--
Oscar


More information about the Python-ideas mailing list