class chararray(ndarray):
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".
astype argsort copy 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 put ravel repeat replace reshape resize rfind rindex rjust rsplit rstrip searchsorted setfield setflags sort split splitlines squeeze startswith strip swapaxes swapcase take title tofile tolist tostring translate transpose upper view zfill
ndarray.strides
for full description).
Default is None.>>> 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')
Method | __add__ |
Return (self + other), that is string concatenation, element-wise for a pair of array_likes of str or unicode. |
Method | __array​_finalize__ |
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 str.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 |
No summary |
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.
add
Return (self % i), that is pre-Python 2.6 string formatting
(interpolation), element-wise for a pair of array_likes of string_
or unicode_
.
mod
Return (self * i), that is string multiple concatenation, element-wise.
multiply
Undocumented
Return (other + self), that is string concatenation,
element-wise for a pair of array_likes of string_
or unicode_
.
add
Return (self * i), that is string multiple concatenation, element-wise.
multiply
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."
>>> 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')
Return a copy of self
with only the first character of each element
capitalized.
char.capitalize
Return a copy of self
with its elements centered in a
string of length width
.
center
Returns an array with the number of non-overlapping occurrences of
substring sub
in the range [start
, end
].
char.count
Returns a boolean array which is True
where the string element
in self
ends with suffix
, otherwise False
.
char.endswith
Return a copy of each string element where all tab characters are replaced by one or more spaces.
char.expandtabs
For each element, return the lowest index in the string where
substring sub
is found.
char.find
Like find
, but raises ValueError
when the substring is not found.
char.index
Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
char.isalnum
Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
char.isalpha
For each element in self
, return True if there are only
decimal characters in the element.
char.isdecimal
Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
char.isdigit
Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
char.islower
For each element in self
, return True if there are only
numeric characters in the element.
char.isnumeric
Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
char.isspace
Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
char.istitle
Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
char.isupper
Return a string which is the concatenation of the strings in the
sequence seq
.
char.join
Return an array with the elements of self
left-justified in a
string of length width
.
char.ljust
Return an array with the elements of self
converted to
lowercase.
char.lower
For each element in self
, return a copy with the leading characters
removed.
char.lstrip
For each element in self
, return a copy of the string with all
occurrences of substring old
replaced by new
.
char.replace
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
].
char.rfind
Like rfind
, but raises ValueError
when the substring sub
is
not found.
char.rindex
Return an array with the elements of self
right-justified in a string of length width
.
char.rjust
For each element in self
, return a list of the words in
the string, using sep
as the delimiter string.
char.rsplit
For each element in self
, return a copy with the trailing
characters removed.
char.rstrip
For each element in self
, return a list of the words in the
string, using sep
as the delimiter string.
char.split
For each element in self
, return a list of the lines in the
element, breaking at line boundaries.
char.splitlines
Returns a boolean array which is True
where the string element
in self
starts with prefix
, otherwise False
.
char.startswith
For each element in self
, return a copy with the leading and
trailing characters removed.
char.strip
For each element in self
, return a copy of the string with
uppercase characters converted to lowercase and vice versa.
char.swapcase
For each element in self
, return a titlecased version of the
string: words start with uppercase characters, all remaining cased
characters are lowercase.
char.title
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.
char.translate