[New-bugs-announce] [issue24068] statistics module - incorrect results with boolean input

Wolfgang Maier report at bugs.python.org
Tue Apr 28 10:53:26 CEST 2015

New submission from Wolfgang Maier:

the mean function in the statistics module gives nonsensical results with boolean values in the input, e.g.:

>>> mean([True, True, False, False])

>>> mean([True, 1027])

This is an issue with the module's internal _sum function that mean relies on. Other functions relying on _sum are affected more subtly, e.g.:

>>> variance([1, 1027, 0])

>>> variance([True, 1027, 0])

The problem with _sum is that it will try to coerce its result to any non-int type found in the input (so bool in the examples), but bool(1028) is just True so information gets lost.

I've attached a patch preventing the type cast when it would be to bool.
I don't have time to write a separate test though so if somebody wants to take over .. :)

components: Library (Lib)
files: statistics._sum.patch
keywords: patch
messages: 242169
nosy: steven.daprano, wolma
priority: normal
severity: normal
status: open
title: statistics module - incorrect results with boolean input
type: behavior
versions: Python 3.4, Python 3.5
Added file: http://bugs.python.org/file39220/statistics._sum.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list