numpy/nan-to-int
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Casting an array that holds NaN to an int type turns each NaN into -9223372036854775808. Keep the float array, or replace NaN with a whole number before the cast.
counts = np.where(seen, totals, np.nan).astype(int)counts = np.where(seen, totals, 0).astype(int)What the rule reports
Section titled “What the rule reports”The rule reports np.where(m, x, np.nan).astype(int), which turns each NaN into -9223372036854775808.
How to fix it
Section titled “How to fix it”With --unsafe-fixes, xpd removes a bare integer cast to keep the NaNs. If your code requires integers, decide how to represent the missing values.
The cast can also issue a RuntimeWarning, and the integer it gives for NaN depends on the platform and dtype.
Diagnostic message
Section titled “Diagnostic message”This casts an array that holds np.nan to $T, and NaN becomes -9223372036854775808. Keep the float array, or replace NaN with a whole number before the cast.See all rules in the NumPy pack.