Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead

Verified FixPython 3.10+NumPy 2.0+Silo: numpy

Quick Fix / Solution Rapide

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

  1. Legacy Scientific Codebases: Code written for NumPy 1.x using dtype=np.string_.
  2. Outdated Third-Party Extensions: Libraries like h5py, tables, or old versions of scikit-learn that relied on np.string_ for fixed-length ASCII columns.
  3. Automatic NumPy 2.0 Upgrades: Running pip install --upgrade numpy in existing environments.

Reproduction Code (MCVE)

Example: Bug Reproduction
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.

Example: Recommended Solution
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.

Example: Alternative Solution
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.