StringDType Deep Dive

NumPy 2.0 introduces np.dtypes.StringDType, a true native string type replacing the historically used object dtype. Strings are stored compactly with UTF-8 support and vectorized operations.

Usage

python
import numpy as np

# Old: dtype=object (slow, fragmented memory)
old = np.array(['alice', 'bob', 'charlie'], dtype=object)

# New: StringDType (compact, fast)
new = np.array(['alice', 'bob', 'charlie'],
               dtype=np.dtypes.StringDType())

print(new.dtype)  # StringDType()
print(new[0])     # 'alice'

# Vectorized operations possible
upper = np.char.upper(new) if hasattr(np.char, 'upper') else new
print(upper)  # ['ALICE', 'BOB', 'CHARLIE']

Sources