You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
implicit ascii encoding for dtypes of kind S #6315
Investigating PyTables/PyTables#499 I came across the following numpy's behaviour that I find unexpected:
In [122]: z=np.zeros(1, dtype='S10') # 'S' is bytes, not a stringIn [123]: zOut[123]:
array([b''],
dtype='|S10')
In [124]: z[0] ='ciao'# enters a stringIn [125]: z[0] # exit encoded bytesOut[125]: b'ciao'In [126]: z[0] ='❤'# and the encoding is just ascii---------------------------------------------------------------------------UnicodeEncodeErrorTraceback (mostrecentcalllast)
<ipython-input-126-387e70d94037>in<module>()
---->1z[0] ='❤'UnicodeEncodeError: 'ascii'codeccan't encode character '\u2764' inposition0: ordinalnotinrange(128)
So it seems that the string 'ciao' automagically becomes 'ciao'.encode('ascii') on its way to z[0] which is surprising and kind of wrong anyway. Said otherwise, I won't expect any implicit conversion between bytes and strings.
Activity
mattip commentedon Aug 28, 2019
Closing. The original reporter should have used
z = np.zeros(1, dtype='U10')to hold encoded strings (U10for unicode)