[Numpy-discussion] checking S versus U dtype

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Jun 1 11:54:13 EDT 2015


What's the best way to check whether a numpy array is string or bytes on
python3?

using char?


>>> A = np.asarray([[1, 0, 0],['E', 1, 0],['E', 'E', 1]], dtype='<U1')
>>> A
array([['1', '0', '0'],
       ['E', '1', '0'],
       ['E', 'E', '1']],
      dtype='<U1')
>>> A.dtype
dtype('<U1')
>>> A.dtype.char
'U'
>>> A.dtype.char == 'U'
True
>>> A.dtype.char == 'S'
False
>>> A.astype('<S1').dtype.char == 'S'
True
>>> A.astype('<S1').dtype.char == 'U'
False
>>>

background:
I don't know why sometimes I got S and sometimes U on Python 3.4, and I
want the code to work with both

>>> A == 'E'
array([[False, False, False],
       [ True, False, False],
       [ True,  True, False]], dtype=bool)

>>> A.astype('<S1') == 'E'
False
>>> A.astype('<S1') == b'E'
array([[False, False, False],
       [ True, False, False],
       [ True,  True, False]], dtype=bool)


Josef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150601/496f81e8/attachment.html>


More information about the NumPy-Discussion mailing list