[NumPy-Tickets] [NumPy] #1163: Incorrect conversion to Int64 by loadtxt (traced to _getconv in numpy.lib.io)

NumPy Trac numpy-tickets@scipy....
Sat Mar 26 09:23:40 CDT 2011


#1163: Incorrect conversion to Int64 by loadtxt (traced to _getconv in
numpy.lib.io)
------------------------------+---------------------------------------------
 Reporter:  onsi              |       Owner:  somebody    
     Type:  defect            |      Status:  needs_review
 Priority:  high              |   Milestone:  Unscheduled 
Component:  numpy.lib         |     Version:  1.2.1       
 Keywords:  io loadtxt int64  |  
------------------------------+---------------------------------------------
Changes (by dynetrekk):

 * cc: paul.anton.letnes@… (added)
  * status:  new => needs_review


Comment:

 I suggest a patch here. The patch will work both for long int as well as
 for converting floats into ints.
 {{{
 diff -r 2763b87dd7e8 -r 8eaaeb6ed8f3 numpy/lib/npyio.py
 --- a/numpy/lib/npyio.py        Fri Mar 25 22:37:19 2011 -0600
 +++ b/numpy/lib/npyio.py        Sat Mar 26 12:40:26 2011 +0100
 @@ -566,7 +566,20 @@
      if issubclass(typ, np.bool_):
          return lambda x: bool(int(x))
      if issubclass(typ, np.integer):
 -        return lambda x: int(float(x))
 +        def _intconv(x):
 +            try:
 +                # This works for long integer, for example:
 +                # >>> int('123456789123456789123456789123456789')
 +                # 123456789123456789123456789123456789L
 +
 +                y = int(x)
 +            except ValueError:
 +                # This will work if the number is a float, for example:
 +                # >>> int(float('1.23e45'))
 +                # 1229999999999999973814869011019624571608236032L
 +                y = int(float(x))
 +            return y
 +        return _intconv
      elif issubclass(typ, np.floating):
          return float
      elif issubclass(typ, np.complex):

 }}}

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1163#comment:4>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list