I have an empty unicode array:
a = np.array([], dtype=np.str_)
I want to encode it:
b = np.char.encode(a, encoding='utf8')
Why is the result an empty array with dtype=float64?
# array([], dtype=float64)
If the array is not empty the resulting array is a properly encoded array with dtype=|S[n]:
a = np.array(['ss', 'ff☆'], dtype=np.str_)
b = np.char.encode(a, encoding='utf8')
# array([b'ss', b'ff\xe2\x98\x86'], dtype='|S5')
EDIT: The accepted answer below does, in fact, answer the question as posed but if you come here looking for a workaround, here is what I did:
if array.size == 0:
encoded_array = np.chararray((0,))
else:
encoded_array = np.char.encode(a, encoding='utf8')
This will produce an empty encoded array with dtype='|S1' if your decoded array is empty.