numpy.nanpercentile() in Python
Last Updated :
01 Jul, 2025
numpy.nanpercentile() function compute the nth percentile of the given data (array elements) along the specified axis and ignores nan values. Example:
Python
import numpy as np
a = np.array([10, 20, np.nan, 40])
res = np.nanpercentile(a, 50)
print(res)
Explanation: The 50th percentile (median) is calculated over [10, 20, 40], ignoring the NaN.
Syntax
numpy.nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=<no value>, method='linear')
Parameters:
Parameter | Description |
---|
a | Input array. |
---|
q | Percentile or sequence of percentiles (between 0 and 100). |
---|
axis | Axis along which percentiles are computed. Default is None (entire array). |
---|
out | Alternative output array. |
---|
overwrite_input | If True, allows modifying input array (for memory efficiency). |
---|
interpolation | [Deprecated] Use method instead. |
---|
keepdims | If True, retains reduced dimensions. |
---|
method | Specifies interpolation method (e.g., 'linear', 'nearest', 'lower', 'higher', 'midpoint'). Default: 'linear'. |
---|
Returns: A scalar or ndarray of percentiles, depending on q and axis.
Examples
Example 1: Computing multiple percentiles
Python
import numpy as np
a = np.array([5, 15, np.nan, 25])
res = np.nanpercentile(a, [25, 50, 75])
print(res)
Explanation: Calculates the 25th, 50th, and 75th percentiles of the non-NaN values.
Example 2: Applying along a specific axis
Python
import numpy as np
a = np.array([[10, 20, np.nan], [30, np.nan, 50]])
res = np.nanpercentile(a, 50, axis=0)
print(res)
Explanation: The median of each column is computed by ignoring NaN values.
- Column 0: median(10, 30) = 20
- Column 1: median(20) = 20
- Column 2: median(50) = 50.
Example 3: Using different interpolation method
Python
import numpy as np
a = np.array([1, 2, 3, 4, 5, np.nan])
res = np.nanpercentile(a, 40, method='nearest')
print(res)
Explanation: With method='nearest', the function picks the value nearest to the 40th percentile position.
Similar Reads
numpy.nanquantile() in Python numpy.nanquantile(arr, q, axis = None) : Compute the qth quantile of the given data (array elements) along the specified axis, ignoring the nan values. Quantiles plays a very important role in statistics. In the figure given above, Q2 is the median and Q3 - Q1 represents the Interquartile Range of
4 min read
numpy.nanprod() in Python numpy.nanprod() function computes the product of array elements over a given axis while treating NaN (Not a Number) values as 1 (i.e., ignoring them in the product). Example:Pythonimport numpy as np a = np.array([1.0, 2.0, np.nan, 4.0]) res = np.nanprod(a) print(res)Output8.0 Explanation: np.nanprod
2 min read
numpy.nanmin() in Python numpy.nanmin()function is used when to returns minimum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmin(arr, axis=None, out=None) Parameters : arr :Input array. axis :Axis along which we want the min value. Otherwise, it will consider
2 min read
numpy.nanvar() in Python numpy.nanvar(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any), while ignoring NaN values. Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of dist
3 min read
numpy.nanargmin() in Python The numpy.nanargmin() function returns indices of the min element of the array in a particular axis ignoring NaNs. The results cannot be trusted if a slice contains only NaNs and Infs. Syntax:  numpy.nanargmin(array, axis = None) Parameters : array : Input array to work on axis : [int, optional]A
2 min read