-1

Env: Python 3.6, Numpy 1.14.0

a = {ndarray}['中','中']

I need to convert str type to numpy.string_.

A single str can be converted like this: np.string_('中', encoding='utf-8')]

But, how to convert a ndarray with a single statement not loop?

a.astype(np.string_) will raise a error:

{SystemError} returned a result with an error set

seizetheday
  • 333
  • 1
  • 6
  • 15

1 Answers1

1

np.char has functions that apply str methods to elements of an array:

In [367]: a = np.array(['中','中'])
In [368]: np.string_(a[0], encoding='utf-8')
Out[368]: b'\xe4\xb8\xad'
In [369]: np.char.encode(a, encoding='utf-8')
Out[369]: array([b'\xe4\xb8\xad', b'\xe4\xb8\xad'], dtype='|S3')

np.string_ is a bytestring, while np.str is unicode (in py3). So this translates a 'U1' array into a 'S3' array.

In [370]: a
Out[370]: array(['中', '中'], dtype='<U1')
hpaulj
  • 221,503
  • 14
  • 230
  • 353