Sorting NaNs
Peter J. Holzer
hjp-python at hjp.at
Sat Jun 2 03:32:05 EDT 2018
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
| | | hjp at hjp.at | management tools.
__/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20180602/deb62d13/attachment.sig>
More information about the Python-list
mailing list