Hello and welcome to this tutorial on Numpy nansum. In this tutorial, we will be learning about the NumPy nansum()
method and also seeing a lot of examples regarding the same. So let us begin!
Also read: NumPy nanprod – A Complete Guide
What is NumPy nansum?
In Python, NaN denotes Not a Number. If we have an array that contains some NaN values and want to find its sum, we can use the nansum()
 method from NumPy.
The nansum()
method in NumPy is a function that returns the sum of the array elements calculated by treating the NaN values in the array as equal to 0. It can be the sum of all the array elements, the sum of the array elements along the rows or the sum of the array elements along the columns.
We will see the examples for each of these in the upcoming section of this tutorial.
Syntax of NumPy nansum
numpy.nansum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Parameter | Description | Required/Optional |
a (array_like) | Input array whose sum is desired. | Required |
axis | Axis along which the sum of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the sum of the entire array is to be returned. | Optional |
dtype (data type) | The data type of the array to be returned. | Optional |
out | An alternative output array in which to place the result. It must have the same shape as the expected output. | Optional |
keepdims (bool) | If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. | Optional |
initial | Starting value for the sum. | Optional |
where | Elements to include in the sum. | Optional |
Returns:
An array with the same shape as a, if the axis is not None, which contains the sum of the elements of a by treating NaN values as 0, along the axis given. If the axis=None, a scalar is returned which is the sum of the whole array.
Examples of Numpy.nansum()
Let’s take a look at some of the examples now.
Sum of the entire array using Numpy.nansum()
1-d array
import numpy as np
a = np.array([10, np.nan, 5, 3, np.nan])
ans = np.nansum(a)
print("a =", a)
print("Sum of the array =", ans)
Output:
a = [10. nan 5. 3. nan]
Sum of the array = 18.0
Here, the sum of the array is calculated by treating all the NaN values as zero. Hence, sum = 10+0+5+3+0 = 18.
2-d array
import numpy as np
a = np.array([[10, np.nan, 5], [np.nan, 2, 6]])
ans = np.nansum(a)
print("a =", a)
print("Sum of the array =", ans)
Output:
a = [[10. nan 5.]
[nan 2. 6.]]
Sum of the array = 23.0
Similar to the above example, sum = 10+0+5+0+2+6 = 23.
Sum along the axis using Numpy.nansum()
Columns-wise Sum
import numpy as np
a = np.array([[10, np.nan, 5],
[np.nan, 2, 6]])
# sum along axis=0 i.e. columns
ans = np.nansum(a, axis = 0)
print("a =", a)
print("Sum of the array =", ans)
Output:
a = [[10. nan 5.]
[nan 2. 6.]]
Sum of the array = [10. 2. 11.]
axis=0 specifies that the sum is to be calculated column-wise.
Column 0 sum = 10+0 = 10
Column 1 sum = 0+2 =2
Column 2 sum = 5+6 = 11
Row-wise Sum
import numpy as np
a = np.array([[10, np.nan, 5],
[np.nan, 2, 6]])
# sum along axis=1 i.e. rows
ans = np.nansum(a, axis = 1)
print("a =", a)
print("Sum of the array =", ans)
Output:
a = [[10. nan 5.]
[nan 2. 6.]]
Sum of the array = [15. 8.]
Treating NaN values as 0,
Row 0 sum = 10+0+5 = 15
Row 1 sum = 0+2+6 = 8
Sum of the array containing infinity
import numpy as np
# array containing +infinity
a = np.array([8, 4, np.nan, np.inf, 13])
# array containing -infinity
b = np.array([8, 4, np.nan, np.NINF, 13])
# array containing +infinity and -infinity
c = np.array([8, 4, np.nan, np.inf, np.NINF, 13])
sum_a = np.nansum(a)
sum_b = np.nansum(b)
sum_c = np.nansum(c)
print("a =", a)
print("Sum of the array a =", sum_a)
print("b =", b)
print("Sum of the array b =", sum_b)
print("c =", c)
print("Sum of the array c =", sum_c)
Output:
a = [ 8. 4. nan inf 13.]
Sum of the array a = inf
b = [ 8. 4. nan -inf 13.]
Sum of the array b = -inf
c = [ 8. 4. nan inf -inf 13.]
Sum of the array c = nan
In the above code, NINF denotes -infinity and inf denote infinity. Note that, if the array contains positive infinity then the sum is positive infinity and if the array contains negative infinity then the sum is negative infinity. If the array contains both positive and negative infinity, then the sum of the array is NaN.
Conclusion
That’s all! In this tutorial, we learned about the Numpy nansum method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.