Skip to content

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)

The rule reports np.where(m, x, np.nan).astype(int), which turns each NaN into -9223372036854775808.

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.

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.