[SciPy-User] numpy: loadtxt with default value

Pierre GM pgmdevlist at gmail.com
Sat Aug 15 18:14:00 EDT 2009


On Aug 15, 2009, at 4:50 PM, Andreas Matthias wrote:

> Pierre GM wrote:
>
>> On Aug 15, 2009, at 4:11 PM, Andreas Matthias wrote:
>>
>>> The documentation of numpy.loadtxt says that the `converters'
>>> parameter can be used to provide a default value for missing
>>> data. However the following doesn't work.
>>>
>>>
>>> import numpy as np
>>> from StringIO import StringIO
>>>
>>> s = StringIO('111  222')
>>> a = np.loadtxt(s,
>>>              dtype = {'names': ('a', 'b', 'c'),
>>>                       'formats': (float, float, float)},
>>>              converters = {2: lambda s: float(s or 0)}
>>>              )
>>
>> You have only 2 columns in s, and gave the converter (and name and
>> format) for a third one ?
>
> Yes. This was intended. This should be the case of a "default
> value for missing data" as described in the documentation

Mmh, I see what you mean: you want to be able to specify in advance  
the number of columns through the dtype (in your case, 3), and use  
some default values when a line has less than the required nb of rows...
That won't work in the current implementation: each line should have  
the proper nb of column, even if the column is empty.
In your example, you use spaces as delimiters, but that interferes  
with the EOL. Try to use a different delimiter if you can.
For example,

s = StringIO('111,  222,')
a = np.loadtxt(s,
               dtype = {'names': ('a', 'b', 'c'),
                        'formats': (float, float, float)},
               converters = {2: lambda s: float(s or 0)},
               delimiter=","
               )

should work




More information about the SciPy-User mailing list