Browsing through older messages I came upon a thread discussing the
treatment of NaNs by median(). Since you have to (partially) sort the
values to compute the median, I played around with sorted():

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> sorted([3, 5, float('NaN'), 1])
[3, 5, nan, 1]

What? Does NaN cause sorted to return the original list?

>>> sorted([3, 5, float('NaN'), 1, 0.5])
[3, 5, nan, 0.5, 1]

Nope. Does it partition the list into sublists, which are sorted
individually?

>>> sorted([3, 5, -8, float('NaN'), 1, 0.5])
[-8, 0.5, 1, 3, 5, nan]
>>> sorted([3, 5, -8, float('NaN'), 1, 0.5, 33])
[-8, 0.5, 1, 3, 5, nan, 33]

Also nope. It looks like NaNs just mess up sorting in an unpredictable
way. Is this the intended behaviour or just an accident of
implementation? (I think it's the latter: I can see how a sort algorithm
which doesn't treat NaN specially would produce such results.)

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | h...@hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>

Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to