I think this would be easier to follow if you rolled the axis being worked on to either the front or the back of the shape. You could even let `np.rollaxis` handle whether the axis parameter is in bounds or not. And since `median` doesn't have a `keepdims` argument, the axis vanishes and you don't even have to roll it back into place.