numpy.char.asarray#

char.asarray(obj,itemsize=None,unicode=None,order=None)[source]#

Convert the input to achararray, copying the data only ifnecessary.

Versus a NumPy array of dtypebytes_ orstr_, thisclass adds the following functionality:

  1. values automatically have whitespace removed from the endwhen indexed

  2. comparison operators automatically remove whitespace from theend when comparing values

  3. vectorized string operations are provided as methods(e.g.chararray.endswith)and infix operators (e.g.+,*,%)

Parameters:
objarray of str or unicode-like
itemsizeint, optional

itemsize is the number of characters per scalar in theresulting array. Ifitemsize is None, andobj is anobject array or a Python list, theitemsize will beautomatically determined. Ifitemsize is provided andobjis of type str or unicode, then theobj string will bechunked intoitemsize pieces.

unicodebool, optional

When true, the resultingchararray can contain Unicodecharacters, when false only 8-bit characters. If unicode isNone andobj is one of the following:

  • achararray,

  • an ndarray of typestr_ orunicode_

  • a Python str or unicode object,

then the unicode setting of the output array will beautomatically determined.

order{‘C’, ‘F’}, optional

Specify the order of the array. If order is ‘C’ (default), then thearray will be in C-contiguous order (last-index varies thefastest). If order is ‘F’, then the returned arraywill be in Fortran-contiguous order (first-index varies thefastest).

Examples

>>>importnumpyasnp>>>np.char.asarray(['hello','world'])chararray(['hello', 'world'], dtype='<U5')
On this page