w3resource

NumPy String: numpy.char.chararray() function

numpy.char.chararray() function

The numpy.char.chararray() is a class that represents a flexible array of strings. It is used to create arrays of strings of a fixed length. The chararray class provides a set of string operations that can be applied element-wise to each string in the array.

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. "+", "*", "%").

Syntax:

numpy.char.chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order=None)

Parameters:

Name Description Required /
Optional
shape: tuple Shape of the array. Required
itemsize: int Length of each array element, in number of characters. Default is 1. Optional
unicode: bool Are the array elements of type unicode (True) or string (False). Default is False. Optional
buffer: bool Memory address of the start of the array data. Default is None, in which case a new array is created. Optional
offset: int Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0. Optional
strides: array_like of ints Strides for the array (see ndarray.strides for full description). Default is None. Optional
order: {‘C’, ‘F’} The order in which the array data is stored in memory: 'C' -> “row major” order (the default), 'F' -> “column major” (Fortran) order. Optional

Return value:

Example: Creating a chararray and assigning a value

>>> import numpy as np
>>> charary = np.chararray((4, 5))
>>> charary[:] = 'x'
>>> charary
chararray([[b'x', b'x', b'x', b'x', b'x'],
       [b'x', b'x', b'x', b'x', b'x'],
       [b'x', b'x', b'x', b'x', b'x'],
       [b'x', b'x', b'x', b'x', b'x']], 
      dtype='|S1')

The above code creates a chararray of shape (4, 5) with the numpy function np.chararray(). Here, the data type is set to '|S1', which means a one-byte string. Then, the [:] operator is used to assign the value 'x' to all the elements of the array. The resulting charary array is a 2-dimensional array of bytes, where each element is the string 'x'.

Example: Creating a chararray and assigning values to it

>> import numpy as np
>>> charary = np.chararray((4, 5))
>>> charary = np.chararray(charary.shape, itemsize=7)
>>> charary[:] = 'abc' 
>>> print(charary)
[[b'abc' b'abc' b'abc' b'abc' b'abc']
 [b'abc' b'abc' b'abc' b'abc' b'abc']
 [b'abc' b'abc' b'abc' b'abc' b'abc']
 [b'abc' b'abc' b'abc' b'abc' b'abc']]

In the given code, a numpy chararray object is created with dimensions (4,5) using the np.chararray() method. The shape of the array is (4,5), which means it has 4 rows and 5 columns. The [:] is used to assign a value of 'x' to all the elements of the chararray.

After that, the chararray is recreated with the same dimensions but with a specified item size of 7 using the np.chararray() method.

The [:] operator is then used to assign a string 'abc' to each element of the chararray. Finally, the resulting chararray is printed.

Python - NumPy Code Editor:

Previous: startswith()
Next: NumPy Data type Home



Follow us on Facebook and Twitter for latest update.