[NumPy-Tickets] [NumPy] #1071: loadtxt fails if the last column contains empty value

NumPy Trac numpy-tickets@scipy....
Wed Mar 30 15:03:35 CDT 2011


#1071: loadtxt fails if the last column contains empty value
---------------------------------+------------------------------------------
 Reporter:  Electrion            |       Owner:  somebody    
     Type:  defect               |      Status:  needs_review
 Priority:  normal               |   Milestone:  1.6.0       
Component:  numpy.lib            |     Version:  devel       
 Keywords:  loadtxt ascii strip  |  
---------------------------------+------------------------------------------

Comment(by derek):

 I am afraid I don't understand how this pertains to the ticket. The
 example above is not affected by the proposed patch; but the missing
 value/mixed data types issue can and could always be handled but
 structured arrays and or converters:

 {{{
 cnv = {1: lambda s: s.strip("\'\"")}
 dt = np.dtype([('x', float), ('name', 'S4')])
 >>>np.loadtxt(StringIO("0 '1'\n2 '3'"), converters=cnv)
 array([[ 0.,  1.],
        [ 2.,  3.]])
 >>> np.loadtxt(StringIO("0 '1'\n2 '3'"), dtype=dt)
 array([(0.0, "'1'"), (2.0, "'3'")],
       dtype=[('x', '<f8'), ('name', '|S4')])
 >>> np.loadtxt(StringIO("0 '1'\n2 '3'"), converters=cnv, dtype=dt)
 array([(0.0, '1'), (2.0, '3')],
       dtype=[('x', '<f8'), ('name', '|S4')])
 }}}
 which is also following the documented use of converters.

 The issue addressed here is rather that
 {{{
 cnv = {1: lambda s: np.float(s.strip() or 'Nan')}
 >>> np.loadtxt(StringIO("0 & 1\n2 & "), delimiter='&', converters=cnv)
 array([[  0.,   1.],
        [  2.,  nan]])
 }}}
 worked,
 and
 {{{
 >>>np.loadtxt(StringIO("0 \t1.5\t1\n2 \t \t 4 "), delimiter='\t',
 converters=cnv)
  Out[1102]:
 array([[  0.,   1.,   1.5],
        [  2.,  nan,   4. ]])
 }}}
 also worked, but
 {{{
 >>>np.loadtxt(StringIO("0 \t1\n2 \t "), delimiter='\t', converters=cnv)

 ValueError                                Traceback (most recent call
 last)

 /sw/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname,
 dtype, comments, delimiter, converters, skiprows, usecols, unpack)
     731             X = np.array(X, dtype=dtype)
     732     else:
 --> 733         X = np.array(X, dtype)
     734
     735     X = np.squeeze(X)

 ValueError: setting an array element with a sequence.
 }}}
 did not. And I don't see a valid reason why it shouldn't, either (but I've
 discussed this above).

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


More information about the NumPy-Tickets mailing list