[New-bugs-announce] [issue35698] Division by 2 in statistics.median

Jonathan Fine report at bugs.python.org
Wed Jan 9 06:46:12 EST 2019


New submission from Jonathan Fine <jfine2358 at gmail.com>:

When len(data) is odd, median returns the average of the two middle values. This average is computed using
        i = n//2
        return (data[i - 1] + data[i])/2

This results in the following behaviour

>>> from fractions import Fraction
>>> from statistics import median
>>> F1 = Fraction(1, 1)

>>> median([1])
1
>>> median([1, 1]) # Example 1.
1.0

>>> median([F1])
Fraction(1, 1)
>>> median([F1, F1])
Fraction(1, 1)

>>> median([2, 2, 1, F1]) # Example 2.
Fraction(3, 2)

>>> median([2, 2, F1, 1]) # Example 3.
1.5

Perhaps, when len(data) is odd, it would be better to test the two middle values for equality. This would resolve Example 1. It would not help with Examples 2 and 3, which might not have a satisfactory solution.

See also issue 33084.

----------
messages: 333305
nosy: jfine2358
priority: normal
severity: normal
status: open
title: Division by 2 in statistics.median
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35698>
_______________________________________


More information about the New-bugs-announce mailing list