numpy.core.defchararray.chararray

class numpy.core.defchararray.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
copy Generic (shallow and deep) copying operations.
count(a, sub[, start, end]) Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].
decode(a[, encoding, errors]) Calls str.decode element-wise.
dump
dumps
encode(a[, encoding, errors]) Calls str.encode element-wise.
endswith(a, suffix[, start, end]) Returns a boolean array which is True where the string element
expandtabs(a[, tabsize]) Return a copy of each string element where all tab characters are replaced by one or more spaces.
fill
find(a, sub[, start, end]) For each element, return the lowest index in the string where substring sub is found.
flatten
getfield
index(a, sub[, start, end]) Like find, but raises ValueError when the substring is not found.
isalnum(a) Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
isalpha(a) Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
isdecimal(a) For each element in a, return True if there are only decimal
isdigit(a) Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
islower(a) Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
isnumeric(a) For each element in a, return True if there are only numeric
isspace(a) Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
istitle(a) Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
isupper(a) Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
item
join(sep, seq) Return a string which is the concatenation of the strings in the sequence seq.
ljust(a, width[, fillchar]) Return an array with the elements of a left-justified in a string of length width.
lower(a) Return an array with the elements of a converted to lowercase.
lstrip(a[, chars]) For each element in a, return a copy with the leading characters removed.
nonzero
put
ravel
repeat
replace(a, old, new[, count]) For each element in a, return a copy of the string with all occurrences of substring old replaced by new.
reshape
resize
rfind(a, sub[, start, end]) For each element in a, return the highest index in the string where substring sub is found, such that sub is contained within [start, end].
rindex(a, sub[, start, end]) Like rfind, but raises ValueError when the substring sub is
rjust(a, width[, fillchar]) Return an array with the elements of a right-justified in a string of length width.
rsplit(a[, sep, maxsplit]) For each element in a, return a list of the words in the string, using sep as the delimiter string.
rstrip(a[, chars]) For each element in a, return a copy with the trailing characters removed.
searchsorted
setfield
setflags
sort
split(a[, sep, maxsplit]) For each element in a, return a list of the words in the string, using sep as the delimiter string.
splitlines(a[, keepends]) For each element in a, return a list of the lines in the element, breaking at line boundaries.
squeeze
startswith(a, prefix[, start, end]) Returns a boolean array which is True where the string element
strip(a[, chars]) For each element in a, return a copy with the leading and trailing characters removed.
swapaxes
swapcase(a) For each element in a, return a copy of the string with uppercase characters converted to lowercase and vice versa.
take
title(a) For each element in a, return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.
tofile
tolist
tostring
translate(a, table[, deletechars]) For each element in a, return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table.
transpose
upper(a) Return an array with the elements of a converted to uppercase.
view
zfill(a, width) Return the numeric string left-filled with zeros in a string of length width.

Previous topic

numpy.core.defchararray.startswith

This Page