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:
- 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.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
itemsize : int, optional
unicode : bool, optional
buffer : int, optional
offset : int, optional
strides : array_like of ints, optional
order : {‘C’, ‘F’}, optional
|
---|
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. |