ENH: Added proper handling of nans for numpy.lib.function_base.median

to close issue #585.

Also added unit test.

This was previously PR #4287 , but I botched the rebase, so I just started over. 

Also related was PR #3908 and #4307