Oh yeah, I should have thrown in these cases:

>>> statistics.median([1, 2, nan, 3, 4])
nan
>>> statistics.median([2, 3, 4, nan, 1])
4

I admit that this issue *never* hits me in particular since I use NumPy and
Pandas widely, and every time I do statistics it is from those packages (or
statsmodels, etc), not the standard library.  But users of the standard
library shouldn't get such peculiar behavior (even if it's not that hard to
understand *why* it behaves as it does).

On Thu, Dec 26, 2019 at 10:31 AM David Mertz <me...@gnosis.cx> wrote:

> This came up in discussion here before, maybe a year ago, I think.  There
> was a decision not to change the implementation, but that seemed like a
> mistake (and the discussion was about broader things).
>
> Anyway, I propose that the obviously broken version of
> `statistics.median()` be replaced with a better implementation.
>
> Python 3.8.0 (default, Nov  6 2019, 21:49:08)
> >>> import numpy as np
> >>> import pandas as pd
> >>> import statistics
> >>> nan = float('nan')
> >>> items1 = [nan, 1, 2, 3, 4]
> >>> items2 = [1, 2, 3, 4, nan]
> >>> statistics.median(items1)
> 2
> >>> statistics.median(items2)
> 3
> >>> np.median(items1)
> nan
> >>> np.median(items2)
> nan
> >>> pd.Series(items1).median()
> 2.5
> >>> pd.Series(items2).median()
> 2.5
>
> The NumPy and Pandas answers are both "reasonable" under slightly
> different philosophies of how to handle bad values.  I think raising an
> exception for NaNs would also be reasonable enough.
>
> The one thing that is NOT reasonable is returning different answers for
> median depending on the order of the elements.
>
> On Thu, Dec 26, 2019 at 10:10 AM Marco Sulla via Python-ideas <
> python-ideas@python.org> wrote:
>
>> So why only mean and not median, that's better for statistics? :D
>>
>> Seriously, if you want it builtin, add it to PYTHONSTARTUP:
>> https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP
>>
>>     from statistics import mean
>> _______________________________________________
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/JNMWEYHKS73AAWYIKOERD6OXX25SSQLM/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7NJUQJUZ5WDSVECL43BTDHRBQUKLDKTH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to