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

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

I hit this one re-running a five-year-old preprocessing script that used to work without complaint. Nothing in the script had changed, only the NumPy version installed under it had, and that was enough to break a line that had been sitting there quietly since a much older project. Below is the reproduction (tested against NumPy 2.4.4), why it only affects some of the old type aliases and not others, and the actual replacement for each one.

Reproducing the Error

The most direct trigger is using np.float (or np.int, np.object, np.str) anywhere a dtype is expected, something that was completely normal NumPy code for years:

import numpy as np

arr = np.array([1, 2, 3], dtype=np.float)
# AttributeError: module 'numpy' has no attribute 'float'.
# `np.float` was a deprecated alias for the builtin `float`. To avoid this
# error in existing code, use `float` by itself. Doing this will not modify
# any behavior and is safe. If you specifically wanted the numpy scalar
# type, use `np.float64` here.

It shows up just as often one step removed from a literal dtype= argument, buried inside a .astype() call on a pandas Series pulled out of a DataFrame:

import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3]})
x = df["a"].values.astype(np.float)
# same AttributeError, same traceback shape, just one more frame from pandas

Why This Happens: Deprecated Aliases, Then a Hard Removal

np.float, np.int, np.bool, np.object, and np.str used to exist purely as convenience aliases pointing at Python's own builtin float, int, bool, object, and str, they added nothing NumPy-specific over just writing the builtin directly. NumPy 1.20 (January 2021) marked them deprecated with a warning, and NumPy 1.24 (December 2022) removed most of them outright. Because DeprecationWarning is silenced by Python's default filters outside __main__, that year-plus warning period passed silently for most codebases, and code that hadn't been touched since before 2021 breaks the moment its environment's NumPy gets upgraded past 1.24, with no warning ever having been seen first.

The tell: if this error appears on code that "used to work," it's almost always a NumPy version bump in the environment, not a code change, check pip show numpy or numpy.__version__ against whatever version the code was originally written against.

Which Aliases Actually Broke, and Which Didn't

Every write-up on this error tends to list the same five names together, but they don't all behave the same way today. Verified directly against NumPy 2.4.4:

Old aliasResult todayReplacement
np.floatRaises AttributeErrorfloat, or np.float64 for the NumPy scalar type
np.intRaises AttributeErrorint, or np.int64/np.int32 for a specific width
np.objectRaises AttributeErrorobject
np.strRaises AttributeErrorstr, or np.str_ for the NumPy scalar type
np.boolDoes not raise — resolves to <class 'numpy.bool'>No change needed; it's now NumPy's own boolean scalar type, not the removed builtin alias
np.longDoes not raise — resolves to <class 'numpy.int64'>No change needed

np.bool is the one that trips people up: it was deprecated in the exact same 1.20 release as the other four, for the exact same reason, but NumPy later repurposed the name instead of deleting it, giving it a real, distinct meaning as NumPy's own boolean type. Code (or a blog post) that treats all five as identically broken will get this one wrong.

The Fix: Replace the Alias, Don't Chase the Version

Pinning numpy<1.24 makes the error go away without touching any code, but it's a stopgap, not a fix, it locks the project out of every NumPy release since December 2022. The actual fix is a direct find-and-replace, and it's safe by NumPy's own description: swapping in the plain Python builtin changes nothing about behavior.

# before
arr = np.array([1, 2, 3], dtype=np.float)

# after — identical behavior
arr = np.array([1, 2, 3], dtype=float)

# or, if a specific NumPy scalar type was actually intended
arr = np.array([1, 2, 3], dtype=np.float64)

A quick way to find every instance across a codebase before deciding which replacement fits each one:

grep -rnE "np\.(float|int|object|str)\b" --include="*.py" .

When the Error Comes From a Library, Not Your Code

If the traceback's last frame before the AttributeError points into site-packages/ rather than a file in the project itself, the removed alias is inside a third-party dependency that hasn't been updated for NumPy 1.24+, not in anything under direct control. Editing the installed package's source is a dead end, it reverts on the next pip install. Two real options in that case:

  • Upgrade the offending library to a release published after NumPy 1.24 shipped (December 2022) — most actively maintained packages fixed this within a few months of the removal.
  • If no fixed version exists yet, pin numpy<1.24 as a deliberate, documented workaround until the dependency catches up, not as the permanent answer.