Open In App

numpy.nanpercentile() in Python

Last Updated : 01 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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)

Output
20.0

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)

Output
[10. 15. 20.]

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)

Output
[20. 20. 50.]

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)

Output
3.0

Explanation: With method='nearest', the function picks the value nearest to the 40th percentile position.


Similar Reads