Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: Check array for string data type

Tags:

How can I determine if a Numpy array contains a string? The array a in

a = np.array('hi world')

has data type dtype('|S8'), where 8 refers to the number of characters in the string.

I don't see how regular expressions (such as re.match('\|S\d+', a.dtype)) would work here as the data type isn't simply '|S8'.

like image 612
Nico Schlömer Avatar asked May 28 '12 20:05

Nico Schlömer


People also ask

How can you identify the datatype of a given NumPy array?

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

How do you check if a string is in a NumPy array?

Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the “in” operator. “in” operator is used to check whether certain element and values are present in a given sequence and hence return Boolean values 'True” and “False“.

Can you have a NumPy array of strings?

The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects.


1 Answers

a.dtype.char == 'S'

or

a.dtype.type is np.string_

See NumPy docs, Data type objects, Attributes.

like image 194
Fred Foo Avatar answered Oct 13 '22 16:10

Fred Foo