Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
python
numpy
communitycreator

What is the numpy.nancumsum() function in NumPy?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The numpy.nancumsum() function in NumPy is used to return the cumulative sum of elements in a given array over a given axis in such a way that NaN values are treated as zeroes.

Syntax

numpy.nancumsum(a, axis=None, dtype=None, out=None)

Parameters

The numpy.nancumsum() function takes the following parameter values:

  • a: This is the input array containing numbers to be computed. This is a required parameter.
  • axis: This is the axis along which the product is determined. This is an optional parameter.
  • dtype: This is the data type of the output array. This is an optional parameter.
  • out: This is the alternate array where the result is placed. This is an optional parameter.

Return value

The numpy.nancumsum() function returns an output array holding the result.

Example

import numpy as np
# creating an array
x = np.array([1, 2, np.nan, 2, np.nan])
# Implementing the nancumsum() function
myarray = np.nancumsum(x, axis=0)
print(x)
print(myarray)
Implementing the numpy.nancumsum() function

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array, x, using the array() method.
  • Line 7: We implement the np.nancumsum() function on the array. The result is assigned to a variable myarray.
  • Line 9: We print the input array x.
  • Line 10: We print the variable myarray.

Note: The NaN values are treated as 0.

RELATED TAGS

function
python
numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring