Skip to content

implicit ascii encoding for dtypes of kind S  #6315

Closed

Description

@andreabedini

Greetings,

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 string

In [123]: z
Out[123]: 
array([b''], 
      dtype='|S10')

In [124]: z[0] = 'ciao' # enters a string

In [125]: z[0]          # exit encoded bytes
Out[125]: b'ciao'

In [126]: z[0] = '❤'    # and the encoding is just ascii
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-126-387e70d94037> in <module>()
----> 1 z[0] = '❤'

UnicodeEncodeError: 'ascii' codec can't encode character '\u2764' in position 0: ordinal not in range(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

  1. mattip commented on Aug 28, 2019

    @mattip
    Member

    Closing. The original reporter should have used z = np.zeros(1, dtype='U10') to hold encoded strings ( U10 for unicode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions