BUG: Added proper handling of median and percentile when nan's are present in array to close issue #586.
Also added unit tests.