Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FutureWarning when comparing a NumPy object to "None"

I have a function that receives some arguments, plus some optional arguments. In it, the action taken is dependent upon whether the optional argument c was filled:

def func(a, b, c = None):

    doStuff()

    if c != None:
        doOtherStuff()

If c is not passed, then this works fine. However, in my context, if c is passed, it will always be a numpy array. And comparing numpy arrays to None yields the following warning:

FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.

So, what is the cleanest and most general way to check whether or not c was passed or not without comparing to None?

like image 316
pretzlstyle Avatar asked Aug 22 '16 20:08

pretzlstyle


People also ask

How do I check if an NP is none?

In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy. all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value.

What does [: :] mean on NumPy arrays?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

How do you compare two NumPy arrays are equal?

Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.

Does NumPy do lazy evaluation?

WeldNumpy is a Weld-enabled library that provides a subclass of NumPy's ndarray module, called weldarray, which supports automatic parallelization, lazy evaluation, and various other optimizations for data science workloads.


1 Answers

Use if c is not None instead. In addition to avoiding the warning, this is generally considered best-practice.

like image 183
jakevdp Avatar answered Sep 28 '22 08:09

jakevdp