NumPy nansum()
The numpy.nansum()
function computes the sum of array elements while treating NaN (Not a Number) values as zero.
This function is useful when working with datasets containing missing values.
Syntax
numpy.nansum(a, axis=None, dtype=None, out=None, keepdims=, initial=<no value>, where=)
Parameters
Parameter | Type | Description |
---|---|---|
a | array_like | Input array containing numerical values. Non-array inputs are converted. |
axis | int, tuple of int, None, optional | Axis or axes along which the sum is computed. By default, the sum is calculated over the entire array. |
dtype | data-type, optional | Defines the data type of the returned array. If omitted, the dtype of a is used. |
out | ndarray, optional | Alternative output array to store the result. Must have the same shape as the expected output. |
keepdims | bool, optional | If True , retains reduced dimensions with size one, enabling broadcasting. |
initial | scalar, optional | Initial value for the sum calculation. Added in NumPy 1.22.0. |
where | array_like of bool, optional | Specifies which elements to include in the sum. |
Return Value
Returns an array containing the sum of elements, treating NaN values as zero. If an output array is provided, the result is stored there.
Examples
1. Summing an Array with NaN Values
Here, we compute the sum of an array containing NaN values, which are treated as zero.
import numpy as np
# Define an array with NaN values
arr = np.array([1, 2, np.nan, 4, np.nan, 6])
# Compute the sum, ignoring NaN values
result = np.nansum(arr)
# Print the result
print("Sum ignoring NaN values:", result)
Output:
Sum ignoring NaN values: 13.0

2. Using the axis
Parameter
Computing the sum along a specific axis in a 2D array containing NaN values.
import numpy as np
# Define a 2D array with NaN values
arr = np.array([[1, np.nan, 3],
[4, 5, np.nan]])
# Compute the sum along axis 0 (column-wise)
result_axis0 = np.nansum(arr, axis=0)
# Compute the sum along axis 1 (row-wise)
result_axis1 = np.nansum(arr, axis=1)
# Print the results
print("Column-wise sum ignoring NaN values:", result_axis0)
print("Row-wise sum ignoring NaN values:", result_axis1)
Output:
Column-wise sum ignoring NaN values: [5. 5. 3.]
Row-wise sum ignoring NaN values: [4. 9.]

3. Using the out
Parameter
Storing the result in a pre-allocated output array instead of creating a new one.
import numpy as np
# Define an array with NaN values
arr = np.array([1, 2, np.nan, 4, 5])
# Create an output array with the same shape
output_array = np.empty(shape=())
# Compute sum and store in output_array
np.nansum(arr, out=output_array)
# Print the result
print("Sum stored in output array:", output_array)
Output:
Sum stored in output array: 12.0

4. Using the where
Parameter
Computing the sum only for selected elements using a boolean mask.
import numpy as np
# Define an array with NaN values
arr = np.array([1, 2, np.nan, 4, 5])
# Define a mask to select specific elements
mask = np.array([True, False, True, True, False])
# Compute sum only for elements where mask is True
result = np.nansum(arr, where=mask)
# Print the result
print("Sum considering only selected elements:", result)
Output:
Sum considering only selected elements: 5.0

The sum is computed only for the elements where mask=True
. Other values are ignored, and NaN values are treated as zero.