nanquantile is hundreds of times slower than quantile for certain cases
Author: ashafferCreated Jun 11, 2020Updated Sep 30, 2026
Labelscomponent: numpy.libProjectsustain-2026
nanquantile seems to be significantly slower than regular quantile, in certain cases. They seem to be identical in the case where you're processing a one dimensional vector, and also identical if you're processing a two dimensional vector as a whole. But if you apply it along one axis of a two dimensional vector, the performance is terrible. I guess it's possible that there's some intrinsic reason for this, but it's hard to imagine what that could be.
Reproducing code example:
import numpy as np
a = np.random.uniform(size=(27, 100))
%timeit np.quantile(a, 0.8, axis=0)
%timeit np.nanquantile(a, 0.8, axis=0)For me the output of this is:
147 µs ± 11.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
10.3 ms ± 953 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)Numpy/Python version information:
1.18.5 3.6.4 (default, Mar 9 2018, 23:15:12)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]Source: numpy/numpy