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.
--
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.