numpy.chararray

class numpy.chararray

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. If one needs arrays of strings, use arrays of dtype object_, string_ or unicode_, and use the free functions in the numpy.char module for fast vectorized string operations.

Versus a regular Numpy array of type str or unicode, 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 or numpy.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 both len(shape) >= 2 and order='Fortran', in which case strides is in “Fortran order”.

Parameters:

shape : tuple

Shape of the array.

itemsize : int, optional

Length of each array element, in number of characters. Default is 1.

unicode : bool, optional

Are the array elements of type unicode (True) or string (False). Default is False.

buffer : int, optional

Memory address of the start of the array data. Default is None, in which case a new array is created.

offset : int, optional

Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0.

strides : array_like of ints, optional

Strides for the array (see ndarray.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

>>> charar = np.chararray((3, 3))
>>> charar[:, :] = 'abc'
>>> charar
chararray([['a', 'a', 'a'],
       ['a', 'a', 'a'],
       ['a', 'a', 'a']],
      dtype='|S1')
>>> charar = np.chararray(charar.shape, itemsize=5)
>>> charar[:, :] = 'abc'
>>> charar
chararray([['abc', 'abc', 'abc'],
       ['abc', 'abc', 'abc'],
       ['abc', 'abc', 'abc']],
      dtype='|S5')

Methods

Previous topic

numpy.memmap.flush

Next topic

numpy.core.defchararray.array

This Page