[Numpy-discussion] percentile function for masked array?

Julian Taylor jtaylor.debian at googlemail.com
Mon Jun 2 11:29:32 EDT 2014


On Mon, Jun 2, 2014 at 3:58 PM, Chao YUE <chaoyuejoy at gmail.com> wrote:
> Dear all,
>
> It seems that there is not a percentile function for masked array in numpy
> or scipy?
> I checked numpy.percentile and scipy.percentile, it seems not support only
> nonmasked array? And there is no percentile function in scipy.stats.mstats,
> so I have to use np.percentile(arr.compressed()) I guess.
>
> Thanks for any comments.
>

there is currently no ma.percentile, but numpy 1.9 which we will
hopefully release as a first beta very soon, will contain
np.nanpercentile which can be used to emulate ma.percentile for
floating point data:

r = np.nanpercentile(maskedarray.filled(np.nan), (5, 95), axis=(0,1))
r = ma.masked_array(r, np.isnan(r))

for 1 dimensional arrays np.percentile(arr.compressed(), (5, 95),
overwrite_input=True) works fine and is also the fastest possible way.


a generic masked percentile would be useful, patches for that are welcome.
Ideally a patch should also take care of the poor performance of
multitimensional nanpercentile along small axes similar to how this PR
fixes it for ma.median/nanmedian:
https://github.com/numpy/numpy/pull/4760



More information about the NumPy-Discussion mailing list