Skip to content

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:

  1. np.float was an alias for Python's built-in float type
  2. It was deprecated in NumPy 1.20 (2021)
  3. It was removed completely in NumPy 1.24 (2022)

This change affects code like:

python
import numpy as np
num = np.float(3)  # Error in NumPy >= 1.24

DEPRECATION TIMELINE

  • NumPy 1.20 (2021): Started issuing deprecation warnings
  • NumPy 1.24 (2022): Permanent removal causing AttributeError

1. Use Python's Built-in float (Most Common Fix)

Replace np.float with Python's native float:

python
num = float(3)  # ✅ Correct replacement

Why 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:

python
# For standard double-precision float (common default)
num = np.float64(3)

# For single-precision float
num = np.float32(3)

3. Alternative NumPy Equivalents

python
# Valid alternatives (choose based on context)
num = np.double(3)     # Alias for float64
num = np.float_(3)     # NumPy scalar equivalent

When The Error Comes From Dependencies

1. Update the Offending Package

Ex: For openpyxl causing the error:

bash
pip install --upgrade openpyxl

2. Downgrade NumPy (Temporary Fix)

bash
pip install "numpy<1.24"  # Reverts to pre-1.24 behavior

WARNING

  • Not recommended for new projects
  • May cause compatibility issues with other packages

3. Check Dependency Compatibility

python
import numpy
print(numpy.__path__)  # Verify installation location
python
# ⚠️ 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 errors

DANGER

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, and np.int is expired.

General Replacement Guide:

DeprecatedRecommended Replacement
np.floatPython float or np.float64
np.intPython int or np.int64
np.boolPython bool
np.objectPython object

For Array Types:

python
# ✅ Proper array declaration with explicit types
arr = np.array([1, 2, 3], dtype=np.float64)  # Explicit
arr = np.array([1, 2, 3], dtype=float)        # Equivalent

Best Practices

  1. Use built-in Python types (int, float, bool) when:

    • Dealing with scalar values
    • Precision isn't critical
    • Writing general-purpose code
  2. 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:

python
print(np.__version__)  # Check before major updates