NumPy module has no attribute 'float' Error
Problem Statement
When using np.float() in NumPy 1.24 or later, you may encounter:
AttributeError: module 'numpy' has no attribute 'float'This occurs because:
np.floatwas an alias for Python's built-infloattype- It was deprecated in NumPy 1.20 (2021)
- It was removed completely in NumPy 1.24 (2022)
This change affects code like:
import numpy as np
num = np.float(3) # Error in NumPy >= 1.24DEPRECATION TIMELINE
- NumPy 1.20 (2021): Started issuing deprecation warnings
- NumPy 1.24 (2022): Permanent removal causing AttributeError
Recommended Solutions
1. Use Python's Built-in float (Most Common Fix)
Replace np.float with Python's native float:
num = float(3) # ✅ Correct replacementWhy this works:
- Behaviorally identical to
np.float - More Pythonic and future-proof
- Works in all NumPy versions
2. Use Explicit NumPy Types (For Specific Precision)
If you need NumPy-specific float types:
# For standard double-precision float (common default)
num = np.float64(3)
# For single-precision float
num = np.float32(3)3. Alternative NumPy Equivalents
# Valid alternatives (choose based on context)
num = np.double(3) # Alias for float64
num = np.float_(3) # NumPy scalar equivalentWhen The Error Comes From Dependencies
1. Update the Offending Package
Ex: For openpyxl causing the error:
pip install --upgrade openpyxl2. Downgrade NumPy (Temporary Fix)
pip install "numpy<1.24" # Reverts to pre-1.24 behaviorWARNING
- Not recommended for new projects
- May cause compatibility issues with other packages
3. Check Dependency Compatibility
import numpy
print(numpy.__path__) # Verify installation locationTemporary Monkey-Patch (Not Recommended)
# ⚠️ Use only for legacy code emergencies
import numpy as np
np.float = float # Reassign attribute
np.int = int # Fixes related int errors
np.object = object # Fixes object errors
np.bool = bool # Fixes bool errorsDANGER
This is a last-resort workaround that:
- Masks fundamental compatibility issues
- May cause undefined behavior
- Will break in future NumPy versions
Explanation of Deprecation
NumPy removed these aliases to avoid confusion between Python built-in types and NumPy-specific dtypes. According to the NumPy 1.24 release notes:
The deprecation for the aliases
np.object,np.bool,np.float,np.complex,np.str, andnp.intis expired.
General Replacement Guide:
| Deprecated | Recommended Replacement |
|---|---|
np.float | Python float or np.float64 |
np.int | Python int or np.int64 |
np.bool | Python bool |
np.object | Python object |
For Array Types:
# ✅ Proper array declaration with explicit types
arr = np.array([1, 2, 3], dtype=np.float64) # Explicit
arr = np.array([1, 2, 3], dtype=float) # EquivalentBest Practices
Use built-in Python types (
int,float,bool) when:- Dealing with scalar values
- Precision isn't critical
- Writing general-purpose code
Use explicit NumPy dtypes (
np.float32,np.int64) when:- Memory optimization needed
- Interfacing with C code
- Implementing performance-critical operations
VERSION COMPATIBILITY TESTING
Always test with np.__version__ before upgrades:
print(np.__version__) # Check before major updates