class chararray(ndarray): (source)
- chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0,
- strides=None, order=None)
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='F', in which case strides
is in "Fortran order".
Methods
dump
dumps
fill
flatten
getfield
item
setfield
setflags
tofile
tolist
tostring
view
Examples
>>> charar = np.chararray((3, 3)) >>> charar[:] = 'a' >>> charar chararray([[b'a', b'a', b'a'], [b'a', b'a', b'a'], [b'a', b'a', b'a']], dtype='|S1')
>>> charar = np.chararray(charar.shape, itemsize=5) >>> charar[:] = 'abc' >>> charar chararray([[b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc']], dtype='|S5')
Parameters | |
shape | Shape of the array. |
itemsize | Length of each array element, in number of characters. Default is 1. |
unicode | Are the array elements of type unicode (True) or string (False). Default is False. |
buffer | Memory address of the start of the array data. Default is None, in which case a new array is created. |
offset | Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0. |
strides | Strides for the array (see ndarray.strides for full description).
Default is None. |
order | The order in which the array data is stored in memory: 'C' -> "row major" order (the default), 'F' -> "column major" (Fortran) order. |
Method | __add__ |
Return (self + other), that is string concatenation, element-wise for a pair of array_likes of str or unicode. |
Method | __array |
Undocumented |
Method | __eq__ |
Return (self == other) element-wise. |
Method | __ge__ |
Return (self >= other) element-wise. |
Method | __getitem__ |
Undocumented |
Method | __gt__ |
Return (self > other) element-wise. |
Method | __le__ |
Return (self <= other) element-wise. |
Method | __lt__ |
Return (self < other) element-wise. |
Method | __mod__ |
Return (self % i), that is pre-Python 2.6 string formatting (interpolation), element-wise for a pair of array_likes of string_ or unicode_ . |
Method | __mul__ |
Return (self * i), that is string multiple concatenation, element-wise. |
Method | __ne__ |
Return (self != other) element-wise. |
Method | __new__ |
Undocumented |
Method | __radd__ |
Return (other + self), that is string concatenation, element-wise for a pair of array_likes of string_ or unicode_ . |
Method | __rmod__ |
Undocumented |
Method | __rmul__ |
Return (self * i), that is string multiple concatenation, element-wise. |
Method | argsort |
Return the indices that sort the array lexicographically. |
Method | capitalize |
Return a copy of self with only the first character of each element capitalized. |
Method | center |
Return a copy of self with its elements centered in a string of length width . |
Method | count |
Returns an array with the number of non-overlapping occurrences of substring sub in the range [start , end ]. |
Method | decode |
Calls bytes.decode element-wise. |
Method | encode |
Calls str.encode element-wise. |
Method | endswith |
Returns a boolean array which is True where the string element in self ends with suffix , otherwise False . |
Method | expandtabs |
Return a copy of each string element where all tab characters are replaced by one or more spaces. |
Method | find |
For each element, return the lowest index in the string where substring sub is found. |
Method | index |
Like find , but raises ValueError when the substring is not found. |
Method | isalnum |
Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise. |
Method | isalpha |
Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise. |
Method | isdecimal |
For each element in self , return True if there are only decimal characters in the element. |
Method | isdigit |
Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise. |
Method | islower |
Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. |
Method | isnumeric |
For each element in self , return True if there are only numeric characters in the element. |
Method | isspace |
Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise. |
Method | istitle |
Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise. |
Method | isupper |
Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise. |
Method | join |
Return a string which is the concatenation of the strings in the sequence seq . |
Method | ljust |
Return an array with the elements of self left-justified in a string of length width . |
Method | lower |
Return an array with the elements of self converted to lowercase. |
Method | lstrip |
For each element in self , return a copy with the leading characters removed. |
Method | partition |
Partition each element in self around sep . |
Method | replace |
For each element in self , return a copy of the string with all occurrences of substring old replaced by new . |
Method | rfind |
For each element in self , return the highest index in the string where substring sub is found, such that sub is contained within [start , end ]. |
Method | rindex |
Like rfind , but raises ValueError when the substring sub is not found. |
Method | rjust |
Return an array with the elements of self right-justified in a string of length width . |
Method | rpartition |
Partition each element in self around sep . |
Method | rsplit |
For each element in self , return a list of the words in the string, using sep as the delimiter string. |
Method | rstrip |
For each element in self , return a copy with the trailing characters removed. |
Method | split |
For each element in self , return a list of the words in the string, using sep as the delimiter string. |
Method | splitlines |
For each element in self , return a list of the lines in the element, breaking at line boundaries. |
Method | startswith |
Returns a boolean array which is True where the string element in self starts with prefix , otherwise False . |
Method | strip |
For each element in self , return a copy with the leading and trailing characters removed. |
Method | swapcase |
For each element in self , return a copy of the string with uppercase characters converted to lowercase and vice versa. |
Method | title |
For each element in self , return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase. |
Method | translate |
For each element in self , 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. |
Method | upper |
Return an array with the elements of self converted to uppercase. |
Method | zfill |
Return the numeric string left-filled with zeros in a string of length width . |
Return (self + other), that is string concatenation, element-wise for a pair of array_likes of str or unicode.
See Also
Return (self % i), that is pre-Python 2.6 string formatting
(interpolation), element-wise for a pair of array_likes of string_
or unicode_
.
See Also
Undocumented
Return (other + self), that is string concatenation,
element-wise for a pair of array_likes of string_
or unicode_
.
See Also
Return the indices that sort the array lexicographically.
For full documentation see numpy.argsort
, for which this method is
in fact merely a "thin wrapper."
Examples
>>> c = np.array(['a1b c', '1b ca', 'b ca1', 'Ca1b'], 'S5') >>> c = c.view(np.chararray); c chararray(['a1b c', '1b ca', 'b ca1', 'Ca1b'], dtype='|S5') >>> c[c.argsort()] chararray(['1b ca', 'Ca1b', 'a1b c', 'b ca1'], dtype='|S5')
Returns an array with the number of non-overlapping occurrences of
substring sub
in the range [start
, end
].
See Also
Return a copy of each string element where all tab characters are replaced by one or more spaces.
See Also
Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
See Also
Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
See Also
Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
See Also
Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
See Also
Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
See Also
Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
See Also
Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
See Also
For each element in self
, return a copy of the string with all
occurrences of substring old
replaced by new
.
See Also
For each element in self
, return the highest index in the string
where substring sub
is found, such that sub
is contained
within [start
, end
].
See Also
For each element in self
, return a list of the words in
the string, using sep
as the delimiter string.
See Also
For each element in self
, return a list of the words in the
string, using sep
as the delimiter string.
See Also
For each element in self
, return a list of the lines in the
element, breaking at line boundaries.
See Also
For each element in self
, return a copy of the string with
uppercase characters converted to lowercase and vice versa.
See Also
For each element in self
, return a titlecased version of the
string: words start with uppercase characters, all remaining cased
characters are lowercase.
See Also
For each element in self
, 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.
See Also