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:

  1. values automatically have whitespace removed from the end when indexed
  2. comparison operators automatically remove whitespace from the end when comparing values
  3. 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:

T

View of the transposed array.

base

Base object if memory is from some other object.

ctypes

An object to simplify the interaction of the array with the ctypes module.

data

Python buffer object pointing to the start of the array’s data.

device

dtype

Data-type of the array’s elements.

flags

Information about the memory layout of the array.

flat

A 1-D iterator over the array.

imag

The imaginary part of the array.

itemset

itemsize

Length of one array element in bytes.

mT

View of the matrix transposed array.

nbytes

Total bytes consumed by the elements of the array.

ndim

Number of array dimensions.

newbyteorder

ptp

real

The real part of the array.

shape

Tuple of array dimensions.

size

Number of elements in the array.

strides

Tuple of bytes to step in each dimension when traversing an array.

Methods