2

I got an numpy array, for example:

myArray = np.array(['a','bc'])

Is it possible use dtype to find out, whether its elements are strings?

The only way I can do is checking myArray.dtype == 'S2', but my Problem is I don't know in advance how many character are there in my elements.

Can I use something like myArray.dtype == 'str'?

1 Answer 1

4

You could use issubdtype to do the checking:

>>> np.issubdtype(myArray.dtype, str)
True

The function checks whether a given dtype is ordered below another in NumPy's type hierarchy.

Alternatively, you could inspect the dtype's character code directly. String types have code 'S':

>>> myArray.dtype.char
'S'
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Wasn't aware of np.issubdtype, I was usually check this stuff as issubclass(myArray.dtype.type, str).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.