Skip to main content
`Ctrl`+`K`

# numpy.char.asarray

char.asarray(obj, itemsize=None, unicode=None, order=None)

  Convert the input to a `chararray`, copying the data only if necessary.

  Deprecated since version 2.5: `chararray` is deprecated. Use an `ndarray` with a string or bytes dtype instead.

  Versus a NumPy array of dtype `bytes_` or `str_`, 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. `chararray.endswith`) and infix operators (e.g. `+`, `*`, `%`)

  Parameters:
    obj array of str or unicode-like
    itemsize int, optional

      itemsize is the number of characters per scalar in the resulting array. If itemsize is None, and obj is an object array or a Python list, the itemsize will be automatically determined. If itemsize is provided and obj is of type str or unicode, then the obj string will be chunked into itemsize pieces.

    unicode bool, optional

      When true, the resulting `chararray` can contain Unicode characters, when false only 8-bit characters. If unicode is None and obj is one of the following:

      - a `chararray`,

      - an ndarray of type `str_` or unicode_

      - a Python str or unicode object,

      then the unicode setting of the output array will be automatically determined.

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

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

  Examples

      >>> import numpy as np
      >>> np.char.asarray(['hello', 'world'])
      chararray(['hello', 'world'], dtype='<U5')