[Numpy-svn] r5495 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Jul 21 19:49:37 CDT 2008


Author: dhuard
Date: 2008-07-21 19:49:31 -0500 (Mon, 21 Jul 2008)
New Revision: 5495

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
Committed patch from Ryan May. It fixes error in loadtxt occurring when usecols is not None and dtypes are given.
I added the test suggested by Ryan.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-07-21 19:19:27 UTC (rev 5494)
+++ trunk/numpy/lib/io.py	2008-07-22 00:49:31 UTC (rev 5495)
@@ -292,8 +292,12 @@
     if converters is None:
         converters = {}
         if dtype.names is not None:
-            converterseq = [_getconv(dtype.fields[name][0]) \
-                            for name in dtype.names]
+            if usecols is None:
+                converterseq = [_getconv(dtype.fields[name][0]) \
+                                for name in dtype.names]
+            else:
+                converters.update([(col,_getconv(dtype.fields[name][0])) \
+                                    for col,name in zip(usecols, dtype.names)])
 
     for i,line in enumerate(fh):
         if i<skiprows: continue

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-07-21 19:19:27 UTC (rev 5494)
+++ trunk/numpy/lib/tests/test_io.py	2008-07-22 00:49:31 UTC (rev 5495)
@@ -220,6 +220,17 @@
         c.seek(0)
         x = np.loadtxt(c, dtype=float, usecols=(1,2))
         assert_array_equal(x, a[:,1:])
+        
+        # Checking with dtypes defined converters. 
+        data = '''JOE 70.1 25.3
+                BOB 60.5 27.9
+                '''
+        c = StringIO.StringIO(data)
+        names = ['stid', 'temp']
+        dtypes = ['S4', 'f8']
+        arr = np.loadtxt(c, usecols=(0,2),dtype=zip(names,dtypes))
+        assert_equal(arr['stid'],  ["JOE",  "BOB"])
+        assert_equal(arr['temp'],  [25.3,  27.9])
 
 
 class Testfromregex(TestCase):



More information about the Numpy-svn mailing list