numpy.char.chararray — NumPy v2.2 Manual (original) (raw)
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.
Note
The chararray class 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 ofdtype object_, bytes_ or str_, and use the free functions in the numpy.char module for fast vectorized string operations.
Versus a NumPy array of dtype bytes_ or str_, 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.array ornumpy.char.asarray, rather than this constructor directly.
This constructor creates the array, using buffer (with _offset_and strides) if it is not None
. If buffer is None
, then constructs a new array with strides in “C order”, unless bothlen(shape) >= 2
and order='F'
, in which case stridesis 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 strides for 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.
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')
Attributes:
View of the transposed array.
Base object if memory is from some other object.
An object to simplify the interaction of the array with the ctypes module.
Python buffer object pointing to the start of the array’s data.
device
Data-type of the array’s elements.
Information about the memory layout of the array.
A 1-D iterator over the array.
The imaginary part of the array.
itemset
Length of one array element in bytes.
View of the matrix transposed array.
Total bytes consumed by the elements of the array.
Number of array dimensions.
newbyteorder
ptp
The real part of the array.
Tuple of array dimensions.
Number of elements in the array.
Tuple of bytes to step in each dimension when traversing an array.
Methods