python - Generic string comparison with numpy -




if have multiple numpy arrays of different string types, such as:

in [411]: x1.dtype out[411]: dtype('s3')  in [412]: x2.dtype out[412]: dtype('<u3')  in [413]: x3.dtype out[413]: dtype('>u5') 

is there way can check whether all strings without having compare each individual type explicitly?

for example, do

in [415]: x1.dtype == <something> out[415]: true  in [416]: x2.dtype == <something> # same above out[416]: true  in [417]: x3.dtype == <something> # same above out[417]: true 

comparing str = no bueno:

in [410]: x3.dtype == str out[410]: false 

one way use np.issubdtype np.character:

np.issubdtype(your_array.dtype, np.character) 

for example:

>>> np.issubdtype('s3', np.character) true  >>> np.issubdtype('<u3', np.character) true  >>> np.issubdtype('>u5', np.character) true 

this numpy dtype hierarchy (as image!) taken numpy documentation. it's helpful if want check common dtype classes:

enter image description here

as can see np.str_ , np.unicode_ both "subclass" np.character.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -