AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead
Replace np.string_ with np.bytes_ (for ASCII byte strings) or standard str / np.str_ (for Unicode text) in your code.
Root Cause Analysis
This error occurs when Python code or a third-party package accesses np.string_ on the numpy namespace after upgrading to NumPy 2.0, where legacy Python 2 compatibility type aliases were permanently removed.
Background: NumPy 2.0 Type Alias Cleanup
In Python 2, str was a byte string and unicode was text. To bridge Python 2 and 3, older versions of NumPy provided aliases:
np.string_-> Alias for byte strings (np.bytes_/bytes)np.unicode_-> Alias for Unicode strings (np.str_/str)np.bool_/np.int_/np.float_
NumPy 2.0.0 (released in June 2024) executed a major cleanup of the top-level namespace to align with modern Python 3 standards. The confusing np.string_ alias was removed because Python developers often mistakenly expected np.string_ to represent Unicode text strings rather than byte arrays.
How the Issue Surfaces
- Legacy Scientific Codebases: Code written for NumPy 1.x using
dtype=np.string_. - Outdated Third-Party Extensions: Libraries like
h5py,tables, or old versions ofscikit-learnthat relied onnp.string_for fixed-length ASCII columns. - Automatic NumPy 2.0 Upgrades: Running
pip install --upgrade numpyin existing environments.
Reproduction Code (MCVE)
import numpy as np
# In NumPy 2.0+, np.string_ was removed and raises AttributeError
if hasattr(np, 'string_'):
raise AttributeError("module 'numpy' has no attribute 'string_'. np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.")
else:
getattr(np, 'string_')
Solution 1: Replace `np.string_` with `np.bytes_` or `np.str_`
Use np.bytes_ if you need byte-encoded ASCII arrays, or np.str_ (or standard str) if you need Unicode text strings.
import numpy as np
# 1. For byte strings: replace np.string_ with np.bytes_
byte_array = np.array([b'alpha', b'beta', b'gamma'], dtype=np.bytes_)
print(f'Byte array with np.bytes_: {byte_array}, dtype={byte_array.dtype}')
# 2. For standard Unicode text strings: use str or np.str_
text_array = np.array(['alpha', 'beta', 'gamma'], dtype=str)
print(f'Text array with str: {text_array}, dtype={text_array.dtype}')
Solution 2: Write Cross-Version Compatible Code
Use getattr(np, 'bytes_', getattr(np, 'string_', None)) or Python standard types for cross-version compatibility.
import numpy as np
# Backward and forward compatible dtype resolver
def get_bytes_dtype():
return getattr(np, 'bytes_', getattr(np, 'string_', bytes))
safe_dtype = get_bytes_dtype()
arr = np.array([b'data_1', b'data_2'], dtype=safe_dtype)
print(f'Cross-version compatible array created with dtype: {arr.dtype}')
Common Pitfalls & Migration Table
| NumPy 1.x Deprecated Alias | NumPy 2.0+ Replacement | Python Native Type |
|---|---|---|
np.string_ |
np.bytes_ |
bytes |
np.unicode_ |
np.str_ |
str |
np.bool8 |
np.bool_ |
bool |
np.int0 / np.uint0 |
np.intp / np.uintp |
int |
Contrasting AttributeError: np.string_ removed with ValueError: numpy.dtype size changed: The attribute error occurs at Python import/attribute lookup; the dtype size error occurs when C-extensions compiled for NumPy 1.x fail ABI compatibility in NumPy 2.0.