class documentation

class chararray(ndarray):

View In Hierarchy

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:

  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='F', in which case strides is in "Fortran order".

Methods

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

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 : object exposing the buffer interface or str, 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([[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.
def __add__(self, other):

Return (self + other), that is string concatenation, element-wise for a pair of array_likes of str or unicode.

See Also

add

def __array_finalize__(self, obj):

Undocumented

def __eq__(self, other):

Return (self == other) element-wise.

See Also

equal

def __ge__(self, other):

Return (self >= other) element-wise.

See Also

greater_equal

def __getitem__(self, obj):

Undocumented

def __gt__(self, other):

Return (self > other) element-wise.

See Also

greater

def __le__(self, other):

Return (self <= other) element-wise.

See Also

less_equal

def __lt__(self, other):

Return (self < other) element-wise.

See Also

less

def __mod__(self, i):

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

mod

def __mul__(self, i):

Return (self * i), that is string multiple concatenation, element-wise.

See Also

multiply

def __ne__(self, other):

Return (self != other) element-wise.

See Also

not_equal

def __new__(subtype, shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order='C'):

Undocumented

def __radd__(self, other):

Return (other + self), that is string concatenation, element-wise for a pair of array_likes of string_ or unicode_.

See Also

add

def __rmod__(self, other):

Undocumented

def __rmul__(self, i):

Return (self * i), that is string multiple concatenation, element-wise.

See Also

multiply

def argsort(self, axis=-1, kind=None, order=None):

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')
def capitalize(self):

Return a copy of self with only the first character of each element capitalized.

See Also

char.capitalize

def center(self, width, fillchar=' '):

Return a copy of self with its elements centered in a string of length width.

See Also

center

def count(self, sub, start=0, end=None):

Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].

See Also

char.count

def decode(self, encoding=None, errors=None):

Calls str.decode element-wise.

See Also

char.decode

def encode(self, encoding=None, errors=None):

Calls str.encode element-wise.

See Also

char.encode

def endswith(self, suffix, start=0, end=None):

Returns a boolean array which is True where the string element in self ends with suffix, otherwise False.

See Also

char.endswith

def expandtabs(self, tabsize=8):

Return a copy of each string element where all tab characters are replaced by one or more spaces.

See Also

char.expandtabs

def find(self, sub, start=0, end=None):

For each element, return the lowest index in the string where substring sub is found.

See Also

char.find

def index(self, sub, start=0, end=None):

Like find, but raises ValueError when the substring is not found.

See Also

char.index

def isalnum(self):

Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.

See Also

char.isalnum

def isalpha(self):

Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.

See Also

char.isalpha

def isdecimal(self):

For each element in self, return True if there are only decimal characters in the element.

See Also

char.isdecimal

def isdigit(self):

Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.

See Also

char.isdigit

def islower(self):

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

char.islower

def isnumeric(self):

For each element in self, return True if there are only numeric characters in the element.

See Also

char.isnumeric

def isspace(self):

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

char.isspace

def istitle(self):

Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.

See Also

char.istitle

def isupper(self):

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

char.isupper

def join(self, seq):

Return a string which is the concatenation of the strings in the sequence seq.

See Also

char.join

def ljust(self, width, fillchar=' '):

Return an array with the elements of self left-justified in a string of length width.

See Also

char.ljust

def lower(self):

Return an array with the elements of self converted to lowercase.

See Also

char.lower

def lstrip(self, chars=None):

For each element in self, return a copy with the leading characters removed.

See Also

char.lstrip

def partition(self, sep):

Partition each element in self around sep.

See Also

partition

def replace(self, old, new, count=None):

For each element in self, return a copy of the string with all occurrences of substring old replaced by new.

See Also

char.replace

def rfind(self, sub, start=0, end=None):

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

char.rfind

def rindex(self, sub, start=0, end=None):

Like rfind, but raises ValueError when the substring sub is not found.

See Also

char.rindex

def rjust(self, width, fillchar=' '):

Return an array with the elements of self right-justified in a string of length width.

See Also

char.rjust

def rpartition(self, sep):

Partition each element in self around sep.

See Also

rpartition

def rsplit(self, sep=None, maxsplit=None):

For each element in self, return a list of the words in the string, using sep as the delimiter string.

See Also

char.rsplit

def rstrip(self, chars=None):

For each element in self, return a copy with the trailing characters removed.

See Also

char.rstrip

def split(self, sep=None, maxsplit=None):

For each element in self, return a list of the words in the string, using sep as the delimiter string.

See Also

char.split

def splitlines(self, keepends=None):

For each element in self, return a list of the lines in the element, breaking at line boundaries.

See Also

char.splitlines

def startswith(self, prefix, start=0, end=None):

Returns a boolean array which is True where the string element in self starts with prefix, otherwise False.

See Also

char.startswith

def strip(self, chars=None):

For each element in self, return a copy with the leading and trailing characters removed.

See Also

char.strip

def swapcase(self):

For each element in self, return a copy of the string with uppercase characters converted to lowercase and vice versa.

See Also

char.swapcase

def title(self):

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

char.title

def translate(self, table, deletechars=None):

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

char.translate

def upper(self):

Return an array with the elements of self converted to uppercase.

See Also

char.upper

def zfill(self, width):

Return the numeric string left-filled with zeros in a string of length width.

See Also

char.zfill