ENH: #586. Improved performance of nan handling in median function.

Also added a flag "check_nan" that can be turned off to regain original
performance.

Also added nanmedian function to calculate the median while ignorning nans.