NumPy nanmin()
The numpy.nanmin()
function returns the minimum value of an array while ignoring any NaN
(Not a Number) values.
It can operate across the entire array or along a specified axis.
Syntax
numpy.nanmin(a, axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
a | array_like | Input array containing numbers whose minimum is required. If not already an array, it will be converted. |
axis | int, tuple of int, None, optional | Axis along which the minimum is computed. By default, the minimum is taken over the entire array. |
out | ndarray, optional | Alternative output array where the result is stored. Must have the same shape as expected output. |
keepdims | bool, optional | If True , reduced dimensions are retained as size one, making broadcasting easier. |
initial | scalar, optional | Specifies the maximum possible value to be considered. Useful when operating on empty slices. |
where | array_like of bool, optional | Defines the condition for selecting elements. Only elements where where=True are included. |
Return Value
Returns an array containing the minimum values of the specified axis while ignoring NaN
values.
If the entire slice contains NaN
, a RuntimeWarning is issued, and NaN
is returned.
Examples
1. Finding the Minimum Value While Ignoring NaNs
We compute the minimum value of an array containing NaN
values.
import numpy as np
# Define an array with NaN values
arr = np.array([3, np.nan, 7, 1, np.nan, 5])
# Compute the minimum while ignoring NaN values
result = np.nanmin(arr)
# Print the result
print("Minimum value ignoring NaN:", result)
Output:
Minimum value ignoring NaN: 1.0

2. Using the axis
Parameter in nanmin()
Here, we compute the minimum along a specific axis while ignoring NaN
values.
import numpy as np
# Define a 2D array with NaN values
arr = np.array([[3, np.nan, 8],
[5, 2, np.nan]])
# Compute the minimum along axis 0 (columns)
result_axis0 = np.nanmin(arr, axis=0)
print("Minimum along columns:", result_axis0)
# Compute the minimum along axis 1 (rows)
result_axis1 = np.nanmin(arr, axis=1)
print("Minimum along rows:", result_axis1)
Output:
Minimum along columns: [3. 2. 8.]
Minimum along rows: [3. 2.]

For axis=0
(columns), the minimum value in each column is computed, ignoring NaN
values.
For axis=1
(rows), the minimum value in each row is computed while ignoring NaN
values.
3. Keeping Dimensions with keepdims=True
Using keepdims=True
ensures that the reduced axis remains as a dimension of size one.
import numpy as np
# Define an array with NaN values
arr = np.array([[np.nan, 4, 7],
[2, np.nan, 1]])
# Compute nanmin with keepdims=True
result = np.nanmin(arr, axis=1, keepdims=True)
# Print the result
print("Minimum values with keepdims=True:", result)
Output:
Minimum values with keepdims=True:
[[4.]
[1.]]

With keepdims=True
, the output retains its original shape, making it compatible for broadcasting.
4. Using the where
Parameter in nanmin()
Using where
to compute the minimum only for selected elements.
import numpy as np
# Define an array with NaN values
arr = np.array([7, np.nan, 3, np.nan, 1])
# Define a mask to specify elements to consider
mask = np.array([True, False, True, False, True])
# Compute nanmin considering only selected elements
result = np.nanmin(arr, where=mask)
# Print the result
print("Minimum considering mask:", result)
Output:
Minimum considering mask: 1.0

Only elements where mask=True
are considered when computing the minimum. The NaN
values and masked elements are ignored.