Nico Schlömer
Nico Schlömer

Reputation: 58791

Numpy: Check array for string data type

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'.

Upvotes: 23

Views: 25246

Answers (1)

Fred Foo
Fred Foo

Reputation: 363567

a.dtype.char == 'S'

or

a.dtype.type is np.string_

for Python 3.x you'll need

a.dtype.type is np.str_

See NumPy docs, Data type objects, Attributes.

Upvotes: 25

Related Questions