NoteRun

AttributeError: module 'numpy' has no attribute 'float'

Python error · NumPy 1.24 and later

np.float was just another name for Python’s own float. NumPy deprecated it in 1.20 and removed it in 1.24 (December 2022), together with np.int, np.object, np.str and np.complex. The rest of the error message says what to use instead.

Why it happens

These names only pointed to Python’s built-in types, and they were easy to confuse with NumPy’s own types such as np.float64. Code written before 2023 often still uses them. NumPy 2.0 (June 2024) removed more names, among them np.NaN, np.Inf, np.float_ and np.product.

How to fix it

Use the built-in name; the code does exactly what it did before:

Before

values = np.array(raw, dtype=np.float)
count = np.int(values.sum())

After

values = np.array(raw, dtype=float)
count = int(values.sum())

If you want NumPy’s own type, write np.float64 or np.int64. For the names NumPy 2.0 removed: np.NaN → np.nan, np.float_ → np.float64, np.product → np.prod.

In NoteRun

NoteRun checks a notebook before you run it and finds these names whatever the notebook called NumPy (np or something else). Fix all replaces every removed name, from NumPy 1.24 and 2.0 alike, in one tap. Nothing changes until you tap, you see every change first, and Undo brings the notebook back.

Download on the App Store

Other errors