pandas/compare-to-missing
| Rule | compare-to-missing |
|---|---|
| Pack | pandas |
| Severity | error |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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().
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
A comparison with a missing value is never true. Write $X.isna() instead.Browse the other rules of the pandas pack.