diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 6b28b4a35874..491a28d224a8 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -979,7 +979,8 @@ def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: - result = np.swapaxes(result, 0, axis) + result = np.rollaxis(result, axis) + if out is not None: out[...] = result return result diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index ac88c4ea506b..989c563d994c 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -711,7 +711,7 @@ def test_multiple_percentiles(self): # For checking consistency in higher dimensional case large_mat = np.ones((3, 4, 5)) large_mat[:, 0:2:4, :] = 0 - large_mat[:, :, 3:] = 2*large_mat[:, :, 3:] + large_mat[:, :, 3:] *= 2 for axis in [None, 0, 1]: for keepdim in [False, True]: with warnings.catch_warnings(record=True) as w: @@ -727,6 +727,9 @@ def test_multiple_percentiles(self): keepdims=keepdim) assert_equal(nan_val, val) + megamat = np.ones((3, 4, 5, 6)) + assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6)) + if __name__ == "__main__": run_module_suite()