bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / dtypes / StringDType
_DTypeMeta
numpy.dtypes:StringDType
source: build-install/usr/lib/python3.14/site-packages/numpy/dtypes.py
Summary
Create a StringDType instance.
Extended Summary
StringDType can be used to store UTF-8 encoded variable-width strings in a NumPy array.
Parameters
na_object: object, optionalObject used to represent missing data. If unset, the array will not use a missing data sentinel.
coerce: bool, optionalWhether or not items in an array-like passed to an array creation function that are neither a str or str subtype should be coerced to str. Defaults to True. If set to False, creating a StringDType array from an array-like containing entries that are not already strings will raise an error.
Examples
import numpy as np
✓from numpy.dtypes import StringDType
✓np.array(["hello", "world"], dtype=StringDType())
✗array(['hello', 'world'], dtype=StringDType())
arr = np.array(["hello", None, "world"], dtype=StringDType(na_object=None))✓
arr
✗array(['hello', None, 'world'], dtype=StringDType(na_object=None))
arr[1] is None
✓True
arr = np.array(["hello", np.nan, "world"], dtype=StringDType(na_object=np.nan))✓
np.isnan(arr)
✗array([False, True, False])
np.array([1.2, object(), "hello world"], dtype=StringDType(coerce=False))✓
Traceback (most recent call last):
File "<doctest numpy.dtypes:StringDType[0]>", line 1, in <module>
np.array([1.2, object(), "hello world"],
~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
dtype=StringDType(coerce=False))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: StringDType only allows string data when string coercion is disabled.
np.array(["hello", "world"], dtype=StringDType(coerce=True))
✗array(['hello', 'world'], dtype=StringDType())
Aliases
numpy.dtypes.StringDType