Skip to main content

Replace == comparison with numpy.isnan()

pixee:python/numpy-nan-equality​

ImportanceReview GuidanceRequires Scanning Tool
LowMerge Without ReviewNo

Comparisons against numpy.nan always result in False. Thus comparing an expression directly against numpy.nan is always unintended. The correct way to compare a value for NaN is to use the numpy.isnan function.

Our changes look something like this:

import numpy as np

a = np.nan
-if a == np.nan:
+if np.isnan(a):
pass

If you have feedback on this codemod, please let us know!

F.A.Q.​

Why is this codemod marked as Merge Without Review?​

We believe any use of == to compare with numpy.nan is unintended given that it is always False. Thus we consider this change safe.

Codemod Settings​

N/A

References​