ENH: Added proper handling of nans for numpy.lib.function_base.median
to close issue #586.

Also added unit test.