
On 28.08.2021 14:33, Richard Damon wrote:
On 8/28/21 6:23 AM, Marc-Andre Lemburg wrote:
To me, the behavior looked a lot like stripping NANs left and right from the list, but what you're explaining makes this appear even more as a bug in the implementation of median() - basically wrong assumptions about NANs sorting correctly. The outcome could be more or less random, it seems.
It isn't a 'bug in median()' making the wrong assumption about NANs sorting, it is an error in GIVING median a NAN which violates its precondition that the input have a total-order by the less than operator.
That precondition is not documented as such, though: https://docs.python.org/3/library/statistics.html#statistics.median
Asking for the median value of a list that doesn't have a proper total order is a nonsense question, so you get a nonsense answer.
Leaving aside that many programmers will probably not know that NANs cause the total ordering of Python floats to fail (even though they are of type float), you'd expect Python to do the right thing and either: - raise an exception or - apply a work-around to regain total ordering, as suggested by Steven, or - return NAN for the calculation as NumPy does.
import statistics statistics.median([1,2,3]) 2 nan = float('nan') statistics.median([1,2,3,nan]) 2.5 statistics.median([1,2,nan,3]) nan statistics.median([1,nan,2,3]) nan statistics.median([nan,1,2,3]) 1.5 nan < 1 False nan < nan False 1 < nan False
vs.
import numpy as np nan = np.nan np.median(np.array([1,2,3,nan])) nan np.median(np.array([1,2,nan,3])) nan np.median(np.array([1,nan,2,3])) nan np.median(np.array([nan,1,2,3])) nan nan < nan False nan < 1 False 1 < nan False
It costs too much to have median test if the input does have a total order, just to try to report this sort of condition, that it won't be done for a general purpose operation.
If NumPy can do it, why not Python ? -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Aug 28 2021)
Python Projects, Coaching and Support ... https://www.egenix.com/ Python Product Development ... https://consulting.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/