- class numpy.char.chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order=None)[source]#
Provides a convenient view on arrays of string and unicode values.
Deprecated since version 2.5:
chararrayis deprecated. Use anndarraywith a string or bytes dtype instead.Note
The
chararrayclass exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays ofdtypeobject_,bytes_orstr_, and use the free functions in thenumpy.charmodule for fast vectorized string operations.Versus a NumPy array of dtype
bytes_orstr_, this class adds the following functionality:values automatically have whitespace removed from the end when indexed
comparison operators automatically remove whitespace from the end when comparing values
vectorized string operations are provided as methods (e.g.
endswith) and infix operators (e.g."+", "*", "%")
chararrays should be created using
numpy.char.arrayornumpy.char.asarray, rather than this constructor directly.This constructor creates the array, using buffer (with offset and
strides) if it is notNone. If buffer isNone, then constructs a new array withstridesin “C order”, unless bothlen(shape) >= 2andorder='F', in which casestridesis in “Fortran order”.- Parameters:
- shapetuple
Shape of the array.
- itemsizeint, optional
Length of each array element, in number of characters. Default is 1.
- unicodebool, optional
Are the array elements of type unicode (True) or string (False). Default is False.
- bufferobject exposing the buffer interface or str, optional
Memory address of the start of the array data. Default is None, in which case a new array is created.
- offsetint, optional
Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0.
- stridesarray_like of ints, optional
Strides for the array (see
stridesfor full description). Default is None.- order{‘C’, ‘F’}, optional
The order in which the array data is stored in memory: ‘C’ -> “row major” order (the default), ‘F’ -> “column major” (Fortran) order.
- Attributes:
TView of the transposed array.
baseBase object if memory is from some other object.
ctypesAn object to simplify the interaction of the array with the ctypes module.
dataPython buffer object pointing to the start of the array’s data.
- device
dtypeData-type of the array’s elements.
flagsInformation about the memory layout of the array.
flatA 1-D iterator over the array.
imagThe imaginary part of the array.
itemsizeLength of one array element in bytes.
mTView of the matrix transposed array.
nbytesTotal bytes consumed by the elements of the array.
ndimNumber of array dimensions.
realThe real part of the array.
shapeTuple of array dimensions.
sizeNumber of elements in the array.
stridesTuple of bytes to step in each dimension when traversing an array.
Methods
Examples
>>> import numpy as np >>> charar = np.char.chararray((3, 3)) >>> charar[:] = 'a' >>> charar chararray([[b'a', b'a', b'a'], [b'a', b'a', b'a'], [b'a', b'a', b'a']], dtype='|S1')
>>> charar = np.char.chararray(charar.shape, itemsize=5) >>> charar[:] = 'abc' >>> charar chararray([[b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc']], dtype='|S5')
numpy.char.chararray — NumPy v2.5.dev0 Manual