[issue18606] Add statistics module to standard library

Alexander Belopolsky report at bugs.python.org
Sun Aug 4 04:07:58 CEST 2013


Alexander Belopolsky added the comment:

> Once we start special-casing types, where will it end?

At the point where all stdlib types are special-cased. :-)


> In the meantime, there's a simple way to do this:

py> from datetime import timedelta as td
py> data = [td(2), td(1), td(3), td(4)]
py> m = statistics.mean([x.total_seconds() for x in data])
py> td(seconds=m)
datetime.timedelta(2, 43200)

Simple, but as simple ways go in this area not correct.  Here is the right way:

py> td.resolution * statistics.mean(d//td.resolution for d in data)
datetime.timedelta(2, 43200)

I wish I had a solution to make sum() work properly on timedeltas without special-casing.  I thought that start could default to type(data[0])(0), but that would bring in strings.  Maybe statistics.mean() should support non-numbers that support addition and division by a number?  Will it be too confusing if mean() supports types that sum() does not?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18606>
_______________________________________


More information about the Python-bugs-list mailing list