numpy: loadtxt with default value
![](https://secure.gravatar.com/avatar/577256f9624f31a7667310e5b5a3d534.jpg?s=120&d=mm&r=g)
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)} ) What's wrong with this code? Ciao Andreas
![](https://secure.gravatar.com/avatar/56b215661867f3b4f4a3b28077de66b3.jpg?s=120&d=mm&r=g)
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)} )
What's wrong with this code?
You have only 2 columns in s, and gave the converter (and name and format) for a third one ?
![](https://secure.gravatar.com/avatar/577256f9624f31a7667310e5b5a3d534.jpg?s=120&d=mm&r=g)
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)} )
What's wrong with this code?
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 converters : {} A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. Ciao Andreas
![](https://secure.gravatar.com/avatar/56b215661867f3b4f4a3b28077de66b3.jpg?s=120&d=mm&r=g)
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
participants (2)
-
Andreas Matthias
-
Pierre GM