numpy.chararray¶
-
class
numpy.chararray[source]¶ Provides a convenient view on arrays of string and unicode values.
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_,string_orunicode_, and use the free functions in thenumpy.charmodule for fast vectorized string operations.Versus a regular Numpy array of type
strorunicode, 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='Fortran', in which casestridesis 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.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.
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')
Attributes
TSame as self.transpose(), except that self is returned if self.ndim < 2. 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. 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. 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
astype(dtype[, order, casting, subok, copy])Copy of the array, cast to a specified type. copy([order])Return a copy of the array. dump(file)Dump a pickle of the array to the specified file. dumps()Returns the pickle of the array as a string. fill(value)Fill the array with a scalar value. flatten([order])Return a copy of the array collapsed into one dimension. getfield(dtype[, offset])Returns a field of the given array as a certain type. item(*args)Copy an element of an array to a standard Python scalar and return it. nonzero()Return the indices of the elements that are non-zero. put(indices, values[, mode])Set a.flat[n] = values[n]for all n in indices.ravel([order])Return a flattened array. repeat(repeats[, axis])Repeat elements of an array. reshape(shape[, order])Returns an array containing the same data with a new shape. resize(new_shape[, refcheck])Change shape and size of array in-place. searchsorted(v[, side, sorter])Find indices where elements of v should be inserted in a to maintain order. setfield(val, dtype[, offset])Put a value into a specified place in a field defined by a data-type. setflags([write, align, uic])Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively. sort([axis, kind, order])Sort an array, in-place. squeeze([axis])Remove single-dimensional entries from the shape of a. swapaxes(axis1, axis2)Return a view of the array with axis1 and axis2 interchanged. take(indices[, axis, out, mode])Return an array formed from the elements of a at the given indices. tofile(fid[, sep, format])Write array to a file as text or binary (default). tolist()Return the array as a (possibly nested) list. tostring([order])Construct Python bytes containing the raw data bytes in the array. transpose(*axes)Returns a view of the array with axes transposed. view([dtype, type])New view of array with the same data. argsort