StringDType en profondeur

NumPy 2.0 introduit np.dtypes.StringDType, un vrai type chaîne de caractères natif remplaant le dtype object utilisé historiquement. Les chaînes sont stockées de manière compacte avec support UTF-8 et opérations vectorisées.

Utilisation

python
import numpy as np

# Ancien : dtype=object (lent, mémoire fragmentée)
old = np.array(['alice', 'bob', 'charlie'], dtype=object)

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

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

# Opérations vectorisées possibles
upper = np.char.upper(new) if hasattr(np.char, 'upper') else new
print(upper)  # ['ALICE', 'BOB', 'CHARLIE']

Sources