[Numpy-discussion] using loadtxt to load a text file in to a numpy array

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Jan 23 14:18:20 EST 2014


On Thu, Jan 23, 2014 at 1:49 PM, Chris Barker <chris.barker at noaa.gov> wrote:

>
> s = 'a string'
> np.array((s,), dtype='S')[0] == s
>
> Gives you False, rather than True on py2. This is because a py3 string is
> translated to the 'S' type (presumable with the default encoding, another
> maybe not a good idea, but returns a bytes object, which does not compare
> true to a py3 string. YOu can work aroudn this with varios calls to encode()
> and decode, and/or using b'a string', but that is ugly, kludgy, and doesn't
> work well with the py3 text model.

I think this is just inconsistent casting rules in numpy,

numpy should either refuse to assign the wrong type, instead of using
the repr as in some of the earlier examples of Oscar

>>> s = np.inf
>>> np.array((s,), dtype=int)[0] == s
Traceback (most recent call last):
  File "<pyshell#126>", line 1, in <module>
    np.array((s,), dtype=int)[0] == s
OverflowError: cannot convert float infinity to integer

or use the **same** conversion/casting rules also during the
interaction with python as are used in assignments and array creation.

Josef



More information about the NumPy-Discussion mailing list