Skip to content

python/float-around-number

Pack Severity Fix Versions
Python warning unsafe, with xpd check --fix --unsafe-fixes All versions

A number from a float64 array or a Series is a float already, and so is arithmetic on it. Drop the float(), and keep it only for text, an int, or a float32.

def cheapest(prices: np.ndarray, sold: np.ndarray):
best = prices.argmin()
return float(prices[best]), float(sold[best] * 2)
return prices[best], sold[best] * 2

The rule reports float() around a subscript, a name, an attribute or arithmetic, in a file that imports numpy or pandas. It leaves alone a string, a value whose name points at text (such as a regex group, a split, a header or the environment), a return inside a function annotated with float, and an assignment annotated with float.

xpd check --fix --unsafe-fixes unwraps float(value) to value (or (value) for arithmetic expressions). It is unsafe because the file does not prove whether the value is text, an integer that needs conversion, or a float32.

A number taken from a float64 array or a Series is an np.float64, and so is arithmetic on it. On NumPy 2.5.5 and pandas 3.1.1, isinstance(value, float) was True for a[0], a[0] * 3, s.iloc[0], s.sum() and s.max(). Keep the call in four cases: the value is text, the value is an int that has to become a float, the array holds float32, or ty requires narrowing from np.floating[Any] (such as np.vdot(a, b)). For NumPy scalars that ty types as np.floating[Any], prefer .item() (which ty types as float and xpd allows everywhere) or float() in a -> float return or : float assignment.

A number from a float64 array or a Series is a float already, and so is arithmetic on it. Drop this float(), and keep it only for text, for an int that has to become a float, or for a float32.

See all rules in the Python pack.