numpy.char.chararray#
- classnumpy.char.chararray(shape,itemsize=1,unicode=False,buffer=None,offset=0,strides=None,order=None)[source]#
Provides a convenient view on arrays of string and unicode values.
Note
The
chararray
class exists for backwards compatibility withNumarray, it is not recommended for new development. Starting from numpy1.4, if one needs arrays of strings, it is recommended to use arrays ofdtype
object_
,bytes_
orstr_
, and usethe free functions in thenumpy.char
module for fast vectorizedstring operations.Versus a NumPy array of dtype
bytes_
orstr_
, thisclass adds the following functionality:values automatically have whitespace removed from the endwhen indexed
comparison operators automatically remove whitespace from theend 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
ornumpy.char.asarray
, rather than this constructor directly.This constructor creates the array, usingbuffer (withoffsetand
strides
) if it is notNone
. Ifbuffer isNone
, thenconstructs a new array withstrides
in “C order”, unless bothlen(shape)>=2
andorder='F'
, in which casestrides
is in “Fortran order”.- Parameters:
- shapetuple
Shape of the array.
- itemsizeint, optional
Length of each array element, in number of characters. Default is 1.
- unicodebool, optional
Are the array elements of type unicode (True) or string (False).Default is False.
- bufferobject 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.
- offsetint, optional
Fixed stride displacement from the beginning of an axis?Default is 0. Needs to be >=0.
- stridesarray_like of ints, optional
Strides for the array (see
strides
forfull 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
>>>importnumpyasnp>>>charar=np.char.chararray((3,3))>>>charar[:]='a'>>>chararchararray([[b'a', b'a', b'a'], [b'a', b'a', b'a'], [b'a', b'a', b'a']], dtype='|S1')
>>>charar=np.char.chararray(charar.shape,itemsize=5)>>>charar[:]='abc'>>>chararchararray([[b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc']], dtype='|S5')
- Attributes:
T
View of 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.
- device
dtype
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.
- itemset
itemsize
Length of one array element in bytes.
mT
View of the matrix transposed array.
nbytes
Total bytes consumed by the elements of the array.
ndim
Number of array dimensions.
- newbyteorder
- ptp
real
The real part of the array.
shape
Tuple of array dimensions.
size
Number of elements in the array.
strides
Tuple of bytes to step in each dimension when traversing an array.
Methods
astype
(dtype[, order, casting, subok, copy])Copy of the array, cast to a specified type.
argsort
([axis, kind, order])Returns the indices that would sort this array.
copy
([order])Return a copy of the array.
count
(sub[, start, end])Returns an array with the number of non-overlapping occurrences of substringsub in the range [start,end].
decode
([encoding, errors])Calls
bytes.decode
element-wise.dump
(file)Dump a pickle of the array to the specified file.
dumps
()Returns the pickle of the array as a string.
encode
([encoding, errors])Calls
str.encode
element-wise.endswith
(suffix[, start, end])Returns a boolean array which isTrue where the string element inself ends withsuffix, otherwiseFalse.
expandtabs
([tabsize])Return a copy of each string element where all tab characters are replaced by one or more spaces.
fill
(value)Fill the array with a scalar value.
find
(sub[, start, end])For each element, return the lowest index in the string where substringsub is found.
flatten
([order])Return a copy of the array collapsed into one dimension.
getfield
(dtype[, offset])Returns a field of the given array as a certain type.
index
(sub[, start, end])Like
find
, but raisesValueError
when the substring is not found.isalnum
()Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
isalpha
()Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
For each element inself, return True if there are only decimal characters in the element.
isdigit
()Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
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.
For each element inself, return True if there are only numeric characters in the element.
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
()Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
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)Copy an element of an array to a standard Python scalar and return it.
join
(seq)Return a string which is the concatenation of the strings in the sequenceseq.
ljust
(width[, fillchar])Return an array with the elements ofself left-justified in a string of lengthwidth.
lower
()Return an array with the elements ofself converted to lowercase.
lstrip
([chars])For each element inself, return a copy with the leading characters removed.
nonzero
()Return the indices of the elements that are non-zero.
put
(indices, values[, mode])Set
a.flat[n]=values[n]
for alln in indices.ravel
([order])Return a flattened array.
repeat
(repeats[, axis])Repeat elements of an array.
replace
(old, new[, count])For each element inself, return a copy of the string with all occurrences of substringold replaced bynew.
reshape
(shape, /, *[, order, copy])Returns an array containing the same data with a new shape.
resize
(new_shape[, refcheck])Change shape and size of array in-place.
rfind
(sub[, start, end])For each element inself, return the highest index in the string where substringsub is found, such thatsub is contained within [start,end].
rindex
(sub[, start, end])Like
rfind
, but raisesValueError
when the substringsub is not found.rjust
(width[, fillchar])Return an array with the elements ofself right-justified in a string of lengthwidth.
rsplit
([sep, maxsplit])For each element inself, return a list of the words in the string, usingsep as the delimiter string.
rstrip
([chars])For each element inself, return a copy with the trailing characters removed.
searchsorted
(v[, side, sorter])Find indices where elements of v should be inserted in a to maintain order.
setfield
(val, dtype[, offset])Put a value into a specified place in a field defined by a data-type.
setflags
([write, align, uic])Set array flags WRITEABLE, ALIGNED, WRITEBACKIFCOPY, respectively.
sort
([axis, kind, order])Sort an array in-place.
split
([sep, maxsplit])For each element inself, return a list of the words in the string, usingsep as the delimiter string.
splitlines
([keepends])For each element inself, return a list of the lines in the element, breaking at line boundaries.
squeeze
([axis])Remove axes of length one froma.
startswith
(prefix[, start, end])Returns a boolean array which isTrue where the string element inself starts withprefix, otherwiseFalse.
strip
([chars])For each element inself, return a copy with the leading and trailing characters removed.
swapaxes
(axis1, axis2)Return a view of the array withaxis1 andaxis2 interchanged.
swapcase
()For each element inself, return a copy of the string with uppercase characters converted to lowercase and vice versa.
take
(indices[, axis, out, mode])Return an array formed from the elements ofa at the given indices.
title
()For each element inself, return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.
tofile
(fid[, sep, format])Write array to a file as text or binary (default).
tolist
()Return the array as an
a.ndim
-levels deep nested list of Python scalars.translate
(table[, deletechars])For each element inself, return a copy of the string where all characters occurring in the optional argumentdeletechars are removed, and the remaining characters have been mapped through the given translation table.
transpose
(*axes)Returns a view of the array with axes transposed.
upper
()Return an array with the elements ofself converted to uppercase.
view
([dtype][, type])New view of array with the same data.
zfill
(width)Return the numeric string left-filled with zeros in a string of lengthwidth.
tostring