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