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. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to 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[:] = 'a'
>>> 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

astype
argsort(a[, axis, kind, order]) Returns the indices that would sort an array.
copy(a) Return an array copy of the given object.
count
decode
dump
dumps
encode
endswith
expandtabs
fill
find
flatten
getfield
index
isalnum
isalpha
isdecimal
isdigit
islower
isnumeric
isspace
istitle
isupper
item
join
ljust
lower
lstrip
nonzero(a) Return the indices of the elements that are non-zero.
put(a, ind, v[, mode]) Replaces specified elements of an array with given values.
ravel(a[, order]) Return a flattened array.
repeat(a, repeats[, axis]) Repeat elements of an array.
replace
reshape(a, newshape[, order]) Gives a new shape to an array without changing its data.
resize(a, new_shape) Return a new array with the specified shape.
rfind
rindex
rjust
rsplit
rstrip
searchsorted(a, v[, side]) Find indices where elements should be inserted to maintain order.
setfield
setflags
sort(a[, axis, kind, order]) Return a sorted copy of an array.
split(ary, indices_or_sections[, axis]) Split an array into multiple sub-arrays of equal size.
splitlines
squeeze(a) Remove single-dimensional entries from the shape of an array.
startswith
strip
swapaxes(a, axis1, axis2) Interchange two axes of an array.
swapcase
take(a, indices[, axis, out, mode]) Take elements from an array along an axis.
title
tofile
tolist
tostring
translate
transpose(a[, axes]) Permute the dimensions of an array.
upper
view
zfill

Previous topic

numpy.memmap.flush

This Page