@@ -5525,7 +5525,11 @@ cannot be safely cast to variable data type""" % attname
55255525 # if data is a string or a bytes object, convert to a numpy string array
55265526 # whose length is equal to the rightmost dimension of the
55275527 # variable.
5528- if type (data) in [str ,bytes]: data = numpy.asarray(data,dtype = ' S' + repr (self .shape[- 1 ]))
5528+ if type (data) in [str ,bytes]:
5529+ if encoding == ' ascii' :
5530+ data = numpy.asarray(data,dtype = ' S' + repr (self .shape[- 1 ]))
5531+ else :
5532+ data = numpy.asarray(data,dtype = ' U' + repr (self .shape[- 1 ]))
55295533 if data.dtype.kind in [' S' ,' U' ] and data.dtype.itemsize > 1 :
55305534 # if data is a numpy string array, convert it to an array
55315535 # of characters with one more dimension.
@@ -6816,15 +6820,12 @@ returns a numpy string array with datatype `'UN'` (or `'SN'`) and shape
68166820 dtype = b.dtype.kind
68176821 if dtype not in [" S" ," U" ]:
68186822 raise ValueError (" type must be string or unicode ('S' or 'U')" )
6819- if encoding in [' none' ,' None' ,' bytes' ]:
6820- bs = b.tobytes()
6821- else :
6822- bs = b.tobytes().decode(encoding)
6823+ bs = b.tobytes()
68236824 slen = int (b.shape[- 1 ])
68246825 if encoding in [' none' ,' None' ,' bytes' ]:
68256826 a = numpy.array([bs[n1:n1+ slen] for n1 in range (0 ,len (bs),slen)],' S' + repr (slen))
68266827 else :
6827- a = numpy.array([bs[n1:n1+ slen] for n1 in range (0 ,len (bs),slen)],' U' + repr (slen))
6828+ a = numpy.array([bs[n1:n1+ slen].decode(encoding) for n1 in range (0 ,len (bs),slen)],' U' + repr (slen))
68286829 a.shape = b.shape[:- 1 ]
68296830 return a
68306831
0 commit comments