Backport 1.8 3851

Backport #3851: `BUG: preserve ndarray subclasses in median`.

Closes #3846.