1

1 Problem

I am practicing 100 Numpy exercises. Question 60 asks for how to tell if a given 2D array has null columns?

I am wondering whether it asks for checking a column fulled with 0 or fulled with nan?

2 Solutions I found

If null columns stands for column with its value all equal to 0, this answer satisfies.

# Author: Warren Weckesser
Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis=0)).any())

It uses a trick of any(). Noticing that if one of values in an array is not equal to 0, np.array().any() will return True. For instance:

np.array([0,-1,0]).any()
[Out]: True

However, if null columns stands for column with its value all equal to nan, then my answer satisfies.

Z = np.random.randint(0,3,(3,10)).astype(float)
Z[:,0] = np.nan
G = np.isnan(Z)
print((G.all(axis=0)).any())

3 Summary

What does null means in python? I think is means nan, but it conflicts with Author(Warren Weckesser)'s answer.

Travis
  • 1,152
  • 9
  • 25

1 Answers1

1

NaN or nan, either way, is the same as null. Meaning that there is no data there.

According to this post,

he main reason to use NaN (over None) is that it can be stored with numpy's float64 dtype, rather than the less efficient object dtype

As you can see, None, np.nan and 0 all behave slightly differently.

print(any(np.array([1, None]))) # True
print(all(np.array([1, None]))) # False

print(any(np.array([1, np.nan]))) # True
print(all(np.array([1, np.nan]))) # True

print(any(np.array([1, 0]))) # True
print(all(np.array([1, 0]))) # False

We can see that np.nan for some reason always evaluates to True.

According to this post, both Python and Numpy evaluate null values as True.

So if a truly null value was in the example, it would be evaluating as true. I think the intent of the exercise was to show use of the any() function and the negation with ~. I agree, the title is a bit off, and he's actually trying to find False values rather than null ones. This can also be realized in that np.random.randint(0,3,(3,10)) will only ever return an array filled with values of 0, 1, or 2 and never False or np.nan.

Cohan
  • 4,384
  • 2
  • 22
  • 40