[Numpy-discussion] long integers in loadtxt -- bad parsing?

Andrew Jaffe a.h.jaffe@gmail....
Thu Apr 8 12:24:25 CDT 2010


Hi,

> I am trying to read some 19-digit integers using loadtxt (or genfromtxt
> -- same problem). The numbers are smaller than the max int64 (and the
> max uint64 -- same problem with either one).
>
> Below, Out[184] shows that python has no problem with the conversion,
> but loadtxt gets the last few digits wrong in Out[185].
>
> Am I doing something stupid?
>
> Yours,
>
> Andrew
>
>
>
> In [175]: import numpy as np
>
> In [176]: np.__version__
> Out[176]: '1.4.0'
>
> In [177]: from StringIO import StringIO
>
> In [178]: fc = """1621174818000457763
> 1621209600996363377
> 1621258907994644735
> 1621296000994995765
> 1621374194996298305
> """
>
> In [184]: long(fc[:19])
> Out[184]: 1621174818000457763L
>
> In [185]: np.loadtxt(StringIO(fc), dtype=np.int64)
> Out[185]:
> array([1621174818000457728, 1621209600996363264, 1621258907994644736,
>          1621296000994995712, 1621374194996298240], dtype=int64)

The slightly hack-ish solution is to explicitly use the python long() 
function as a converter:

In [215]: np.loadtxt(StringIO(fc), dtype=np.int64, converters={0:long})
Out[215]:
array([1621174818000457763, 1621209600996363377, 1621258907994644735,
        1621296000994995765, 1621374194996298305], dtype=int64)





More information about the NumPy-Discussion mailing list