[Numpy-discussion] A one-byte string dtype?
aldcroft at head.cfa.harvard.edu
Fri Jan 17 18:05:16 EST 2014
On Fri, Jan 17, 2014 at 5:30 PM, Chris Barker <chris.barker at noaa.gov> wrote:
> I've been blathering away on the related threads a lot -- sorry if it's
> too much. It's gotten a bit tangled up, so I thought I'd start a new one to
> address this one question (i.e. dont bring up genfromtext here):
> Would it be a good thing for numpy to have a one-byte--per-character
> string type?
> We did have that with the 'S' type in py2, but the changes in py3 have
> made it not quite the right thing. And it appears that enough people use
> 'S' in py3 to mean 'bytes', so that we can't change that now.
> The only difference may be that 'S' currently auto translates to a bytes
> object, resulting in things like:
> np.array(['some text',], dtype='S') == 'some text'
> yielding False on Py3. And you can't do all the usual text stuff with the
> resulting bytes object, either. (and it probably used the default encoding
> to generate the bytes, so will barf on some inputs, though that may be
> unavoidable.) So you need to decode the bytes that are given back, and now
> that I think about it, I have no idea what encoding you'd need to use in
> the general case.
> So the correct solution is (particularly on py3) to use the 'U' (unicode)
> dtype for text in numpy arrays.
> However, the 'U' dtype is 4 bytes per character, and that may be "too big"
> for some use-cases. And there is a lot of text in scientific data sets that
> are pure ascii, or at least some 1-byte-per-character encoding.
> So, in the spirit of having multiple numeric types that use different
> amounts of memory, and can hold different ranges of values, a one-byte-per
> character dtype would be nice:
> (note, this opens the door for a 2-byte per (UCS-2) dtype too, I
> personally don't think that's worth it, but maybe that's because I'm an
> english speaker...)
> It could use the 's' (lower-case s) type identifier.
> For passing to/from python built-in objects, it would
> * Allow either Python bytes objects or Python unicode objects as input
> a) bytes objects would be passed through as-is
> b) unicode objects would be encoded as latin-1
> [note: I'm not entirely sure that bytes objects should be allowed, but it
> would provide an nice efficiency in a fairly common case]
> * It would create python unicode text objects, decoded as latin-1.
> Could we have a way to specify another encoding? I'm not sure how that
> would fit into the dtype system.
> I've explained the latin-1 thing on other threads, but the short version
> - It will work perfectly for ascii text
> - It will work perfectly for latin-1 text (natch)
> - It will never give you an UnicodeEncodeError regardless of what
> arbitrary bytes you pass in.
> - It will preserve those arbitrary bytes through a encoding/decoding
> (it still wouldn't allow you to store arbitrary unicode -- but that's the
> limitation of one-byte per character...)
> Bad idea all around: shut up already!
> Fine idea, but who's going to write the code? not me!
> We really should do this.
As evident from what I said in the previous thread, YES, this should really
One important feature would be changing the dtype from 'S' to 's' without
any memory copies, so that conversion would be very cheap. Maybe this
would essentially come for free with something like astype('s', copy=False).
> (of course, with the options of amending the above not-very-fleshed out
> Christopher Barker, Ph.D.
> Emergency Response Division
> NOAA/NOS/OR&R (206) 526-6959 voice
> 7600 Sand Point Way NE (206) 526-6329 fax
> Seattle, WA 98115 (206) 526-6317 main reception
> Chris.Barker at noaa.gov
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion