numpy/numpy · error · ValueError
Can only create a chararray from string data.
Error message
Can only create a chararray from string data.
What it means
Raised in chararray.__array_finalize__ when the array's dtype kind is not one of V/S/U/b/c (void, bytes, unicode, bytes-native, character). chararray is a string-only subclass; numpy refuses to finalize it over numeric or object data. Guard at defchararray.py:592.
Source
Thrown at numpy/_core/defchararray.py:593
offset=offset, strides=strides,
order=order)
if filler is not None:
self[...] = filler
return self
def __array_wrap__(self, arr, context=None, return_scalar=False):
# When calling a ufunc (and some other functions), we return a
# chararray if the ufunc output is a string-like array,
# or an ndarray otherwise
if arr.dtype.char in "SUbc":
return arr.view(type(self))
return arr
def __array_finalize__(self, obj):
# The b is a special case because it is used for reconstructing.
if self.dtype.char not in 'VSUbc':
raise ValueError("Can only create a chararray from string data.")
def __getitem__(self, obj):
val = ndarray.__getitem__(self, obj)
if isinstance(val, character):
return val.rstrip()
return val
# IMPLEMENTATION NOTE: Most of the methods of this class are
# direct delegations to the free functions in this module.
# However, those that return an array of strings should instead
# return a chararray, so some extra wrapping is required.
def __eq__(self, other):
"""
Return (self == other) element-wise.
See Also
--------View on GitHub (pinned to e117b3ca4e)
Solutions
- Convert the data to a string dtype first, then view as chararray: arr.astype('U').view(np.char.chararray).
- Use np.char.asarray(arr) which handles dtype conversion explicitly instead of bare .view.
- Drop the chararray subclass and operate on a plain ndarray of str_/bytes_ dtype, which is the modern recommendation.
Example fix
// before
np.array([1, 2, 3]).view(np.char.chararray)
// after
np.array([1, 2, 3]).astype('U').view(np.char.chararray) Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def to_chararray(arr):
a = np.asarray(arr)
if a.dtype.char not in 'VSUbc':
a = a.astype('U')
return a.view(np.char.chararray) Type guard
def is_string_dtype(arr) -> bool:
return np.asarray(arr).dtype.char in 'VSUbc' Prevention
- Convert to a string dtype before calling .view(np.char.chararray).
- Prefer np.char.asarray or plain str_/bytes_ ndarrays over manual chararray views.
- Avoid subclassing/viewing chararray for numeric data; chararray is legacy and string-only.
When it happens
Trigger: Viewing or slicing a non-string ndarray as a chararray (e.g. arr.view(np.char.chararray) on an int/float array), or constructing/reshaping that triggers __array_finalize__ with a non-string dtype.
Common situations: Migrating legacy np.char usage onto numeric arrays; using .astype after .view in the wrong order; copy/slicing operations that propagate a chararray type onto data of a different dtype.
Related errors
- Can only multiply by integers
- min_digits must be less than or equal to precision
- precision must be greater than 0 if fractional=False
- No input operands
- Character {s} is not a valid symbol.
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/0f0a1feff77dee64.
Report an issue: GitHub.