pandas/compare-to-missing
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
A comparison with a missing value, such as df["b"] == np.nan, is never true. Test for missing values with isna().
missing = df["load"] == np.nanmissing = df["load"].isna()What the rule reports
Section titled “What the rule reports”The rule reports df["b"] == np.nan, df["k"].astype(str) == "nan" and df[df["b"] == None], which are never true. It reports None only inside a subscript, because config["mode"] == None on a dict is valid Python. When the file shows that the value comes from NumPy, as in x = np.arange(3.0) and then x == np.nan, the fix writes pd.isna(x), because a NumPy array has no .isna().
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes df["b"].isna().
If the value is a scalar, the .isna() replacement can raise AttributeError.
Diagnostic message
Section titled “Diagnostic message”A comparison with a missing value is never true. Write $X.isna() instead.See all rules in the pandas pack.