numpy.char.
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.
dtype
object_
string_
unicode_
numpy.char
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. "+", "*", "%")
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. "+", "*", "%")
endswith
"+", "*", "%"
chararrays should be created using numpy.char.array or numpy.char.asarray, rather than this constructor directly.
numpy.char.array
numpy.char.asarray
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”.
strides
None
len(shape) >= 2
order='F'
Shape of the array.
Length of each array element, in number of characters. Default is 1.
Are the array elements of type unicode (True) or string (False). Default is False.
Memory address of the start of the array data. Default is None, in which case a new array is created.
Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0.
Strides for the array (see ndarray.strides for full description). Default is None.
ndarray.strides
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')
T
The transposed array.
base
Base object if memory is from some other object.
ctypes
An object to simplify the interaction of the array with the ctypes module.
data
Python buffer object pointing to the start of the array’s data.
Data-type of the array’s elements.
flags
Information about the memory layout of the array.
flat
A 1-D iterator over the array.
imag
The imaginary part of the array.
itemsize
Length of one array element in bytes.
nbytes
Total bytes consumed by the elements of the array.
ndim
Number of array dimensions.
real
The real part of the array.
shape
Tuple of array dimensions.
size
Number of elements in the array.
Tuple of bytes to step in each dimension when traversing an array.
Methods
astype(dtype[, order, casting, subok, copy])
astype
Copy of the array, cast to a specified type.
argsort([axis, kind, order])
argsort
Returns the indices that would sort this array.
copy([order])
copy
Return a copy of the array.
count(sub[, start, end])
count
Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].
decode([encoding, errors])
decode
Calls str.decode element-wise.
dump(file)
dump
Dump a pickle of the array to the specified file.
dumps()
dumps
Returns the pickle of the array as a string.
encode([encoding, errors])
encode
Calls str.encode element-wise.
endswith(suffix[, start, end])
Returns a boolean array which is True where the string element in self ends with suffix, otherwise False.
expandtabs([tabsize])
expandtabs
Return a copy of each string element where all tab characters are replaced by one or more spaces.
fill(value)
fill
Fill the array with a scalar value.
find(sub[, start, end])
find
For each element, return the lowest index in the string where substring sub is found.
flatten([order])
flatten
Return a copy of the array collapsed into one dimension.
getfield(dtype[, offset])
getfield
Returns a field of the given array as a certain type.
index(sub[, start, end])
index
Like find, but raises ValueError when the substring is not found.
isalnum()
isalnum
Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
isalpha()
isalpha
Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
isdecimal()
isdecimal
For each element in self, return True if there are only decimal characters in the element.
isdigit()
isdigit
Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
islower()
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.
isnumeric()
isnumeric
For each element in self, return True if there are only numeric characters in the element.
isspace()
isspace
Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
istitle()
istitle
Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
isupper()
isupper
Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
item(*args)
item
Copy an element of an array to a standard Python scalar and return it.
join(seq)
join
Return a string which is the concatenation of the strings in the sequence seq.
ljust(width[, fillchar])
ljust
Return an array with the elements of self left-justified in a string of length width.
lower()
lower
Return an array with the elements of self converted to lowercase.
lstrip([chars])
lstrip
For each element in self, return a copy with the leading characters removed.
nonzero()
nonzero
Return the indices of the elements that are non-zero.
put(indices, values[, mode])
put
Set a.flat[n] = values[n] for all n in indices.
a.flat[n] = values[n]
ravel([order])
ravel
Return a flattened array.
repeat(repeats[, axis])
repeat
Repeat elements of an array.
replace(old, new[, count])
replace
For each element in self, return a copy of the string with all occurrences of substring old replaced by new.
reshape(shape[, order])
reshape
Returns an array containing the same data with a new shape.
resize(new_shape[, refcheck])
resize
Change shape and size of array in-place.
rfind(sub[, start, end])
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].
rindex(sub[, start, end])
rindex
Like rfind, but raises ValueError when the substring sub is not found.
rjust(width[, fillchar])
rjust
Return an array with the elements of self right-justified in a string of length width.
rsplit([sep, maxsplit])
rsplit
For each element in self, return a list of the words in the string, using sep as the delimiter string.
rstrip([chars])
rstrip
For each element in self, return a copy with the trailing characters removed.
searchsorted(v[, side, sorter])
searchsorted
Find indices where elements of v should be inserted in a to maintain order.
setfield(val, dtype[, offset])
setfield
Put a value into a specified place in a field defined by a data-type.
setflags([write, align, uic])
setflags
Set array flags WRITEABLE, ALIGNED, (WRITEBACKIFCOPY and UPDATEIFCOPY), respectively.
sort([axis, kind, order])
sort
Sort an array in-place.
split([sep, maxsplit])
split
splitlines([keepends])
splitlines
For each element in self, return a list of the lines in the element, breaking at line boundaries.
squeeze([axis])
squeeze
Remove axes of length one from a.
startswith(prefix[, start, end])
startswith
Returns a boolean array which is True where the string element in self starts with prefix, otherwise False.
strip([chars])
strip
For each element in self, return a copy with the leading and trailing characters removed.
swapaxes(axis1, axis2)
swapaxes
Return a view of the array with axis1 and axis2 interchanged.
swapcase()
swapcase
For each element in self, return a copy of the string with uppercase characters converted to lowercase and vice versa.
take(indices[, axis, out, mode])
take
Return an array formed from the elements of a at the given indices.
title()
title
For each element in self, return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.
tofile(fid[, sep, format])
tofile
Write array to a file as text or binary (default).
tolist()
tolist
Return the array as an a.ndim-levels deep nested list of Python scalars.
a.ndim
tostring([order])
tostring
A compatibility alias for tobytes, with exactly the same behavior.
tobytes
translate(table[, deletechars])
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.
transpose(*axes)
transpose
Returns a view of the array with axes transposed.
upper()
upper
Return an array with the elements of self converted to uppercase.
view([dtype][, type])
view
New view of array with the same data.
zfill(width)
zfill
Return the numeric string left-filled with zeros in a string of length width.