encode
is a string method, so can't work directly on an array of strings. But there are several ways of applying it to each string
Here I'm working Py3, so the default is unicode.
In [179]: A=np.array(['one','two'])
In [180]: A
Out[180]:
array(['one', 'two'],
dtype='<U3')
plain iteration:
In [181]: np.array([s.encode() for s in A])
Out[181]:
array([b'one', b'two'],
dtype='|S3')
np.char
has functions that apply string methods to each element of an array:
In [182]: np.char.encode(A)
Out[182]:
array([b'one', b'two'],
dtype='|S3')
but it looks like this is one of the conversions that astype
can handle:
In [183]: A.astype('<S3')
Out[183]:
array([b'one', b'two'],
dtype='|S3')
And inspired by a recent question about np.chararray
:
What happened to numpy.chararray
In [191]: Ac=np.char.array(A)
In [192]: Ac
Out[192]:
chararray(['one', 'two'],
dtype='<U3')
In [193]: Ac.encode()
Out[193]:
array([b'one', b'two'],
dtype='|S3')