@@ -2170,7 +2170,7 @@ def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',',
21702170 data type. When set to zero all rows are validated.
21712171
21722172 converterd, if not None, is a dictionary mapping column number or
2173- munged column name to a converter function
2173+ munged column name to a converter function.
21742174
21752175 names, if not None, is a list of header names. In this case, no
21762176 header will be read from the file
@@ -2256,11 +2256,18 @@ def newfunc(name, val):
22562256 return func (val )
22572257 return newfunc
22582258
2259+
2260+ def mybool (x ):
2261+ if x == 'True' : return True
2262+ elif x == 'False' : return False
2263+ else : raise ValueError ('invalid bool' )
2264+
22592265 dateparser = dateutil .parser .parse
22602266 mydateparser = with_default_value (dateparser , datetime .date (1 ,1 ,1 ))
22612267 myfloat = with_default_value (float , np .nan )
22622268 myint = with_default_value (int , - 1 )
22632269 mystr = with_default_value (str , '' )
2270+ mybool = with_default_value (mybool , None )
22642271
22652272 def mydate (x ):
22662273 # try and return a date object
@@ -2273,7 +2280,7 @@ def mydate(x):
22732280
22742281 def get_func (name , item , func ):
22752282 # promote functions in this order
2276- funcmap = {myint :myfloat , myfloat :mydate , mydate :mydateparser , mydateparser :mystr }
2283+ funcmap = {mybool : myint , myint :myfloat , myfloat :mydate , mydate :mydateparser , mydateparser :mystr }
22772284 try : func (name , item )
22782285 except :
22792286 if func == mystr :
@@ -2294,7 +2301,7 @@ def get_converters(reader):
22942301 converters = None
22952302 for i , row in enumerate (reader ):
22962303 if i == 0 :
2297- converters = [myint ]* len (row )
2304+ converters = [mybool ]* len (row )
22982305 if checkrows and i > checkrows :
22992306 break
23002307 #print i, len(names), len(row)
@@ -2308,6 +2315,9 @@ def get_converters(reader):
23082315 func = converters [j ]
23092316 if len (item .strip ()):
23102317 func = get_func (name , item , func )
2318+ else :
2319+ # how should we handle custom converters and defaults?
2320+ func = with_default_value (func , None )
23112321 converters [j ] = func
23122322 return converters
23132323
@@ -2427,6 +2437,13 @@ def toval(self, x):
24272437 def fromstr (self , s ):
24282438 return int (s )
24292439
2440+ class FormatBool (FormatObj ):
2441+ def toval (self , x ):
2442+ return x
2443+
2444+ def fromstr (self , s ):
2445+ return bool (s )
2446+
24302447class FormatPercent (FormatFloat ):
24312448 def __init__ (self , precision = 4 ):
24322449 FormatFloat .__init__ (self , precision , scale = 100. )
@@ -2465,6 +2482,7 @@ def fromstr(self, x):
24652482
24662483
24672484defaultformatd = {
2485+ np .bool_ : FormatBool (),
24682486 np .int16 : FormatInt (),
24692487 np .int32 : FormatInt (),
24702488 np .int64 : FormatInt (),
0 commit comments