Comparing numpy arrays containing NaN

For versions of numpy prior to 1.19, this is probably the best approach in situations that don’t specifically involve unit tests:

>>> ((a == b) | (numpy.isnan(a) & numpy.isnan(b))).all()
True

However, modern versions provide the array_equal function with a new keyword argument, equal_nan, which fits the bill exactly.

This was first pointed out by flyingdutchman; see his answer below for details.

Leave a Comment

tech