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

numpy-svn@scip... numpy-svn@scip...
Sat Feb 14 16:42:32 CST 2009


Author: pierregm
Date: 2009-02-14 16:42:29 -0600 (Sat, 14 Feb 2009)
New Revision: 6368

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test__iotools.py
   trunk/numpy/lib/tests/test_io.py
Log:
* genfromtxt : fixed case when using explicit converters and explicit dtype.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2009-02-14 22:38:32 UTC (rev 6367)
+++ trunk/numpy/lib/io.py	2009-02-14 22:42:29 UTC (rev 6368)
@@ -837,6 +837,7 @@
     missing_values = [_.missing_values for _ in converters]
 
     # Update the converters to use the user-defined ones
+    uc_update = []
     for (i, conv) in user_converters.iteritems():
         # If the converter is specified by column names, use the index instead
         if _is_string_like(i):
@@ -850,6 +851,9 @@
         converters[i].update(conv, default=None, 
                              missing_values=missing_values[i],
                              locked=True)
+        uc_update.append((i, conv))
+    # Make sure we have the corrected keys in user_converters...
+    user_converters.update(uc_update)
 
     # Reset the names to match the usecols
     if (not first_line) and usecols:
@@ -960,8 +964,14 @@
                         descr.append(('', ttype))
                     else:
                         descr.append(('', dtype))
+                # So we changed the dtype ?
                 if not ishomogeneous:
-                    dtype = np.dtype(descr)
+                    # We have more than one field
+                    if len(descr) > 1:
+                        dtype = np.dtype(descr)
+                    # We have only one field: drop the name if not needed.
+                    else:
+                        dtype = np.dtype(ttype)
             #
             output = np.array(data, dtype)
             if usemask:

Modified: trunk/numpy/lib/tests/test__iotools.py
===================================================================
--- trunk/numpy/lib/tests/test__iotools.py	2009-02-14 22:38:32 UTC (rev 6367)
+++ trunk/numpy/lib/tests/test__iotools.py	2009-02-14 22:42:29 UTC (rev 6368)
@@ -142,6 +142,14 @@
         assert_equal(test, date(2009, 01, 01))
         test = convert('')
         assert_equal(test, date(2000, 01, 01))
+    #
+    def test_string_to_object(self):
+        "Make sure that string-to-object functions are properly recognized"
+        from datetime import date
+        import time
+        conv = StringConverter(lambda s: date(*(time.strptime(s)[:3])))
+        assert_equal(conv._mapper[-2][0](0), 0j)
+        assert(hasattr(conv, 'default'))
 
 
 #-------------------------------------------------------------------------------

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2009-02-14 22:38:32 UTC (rev 6367)
+++ trunk/numpy/lib/tests/test_io.py	2009-02-14 22:42:29 UTC (rev 6368)
@@ -573,7 +573,7 @@
 
 
     def test_dtype_with_object(self):
-        "Test using an explicit dtype qith an object"
+        "Test using an explicit dtype with an object"
         from datetime import date
         import time
         data = """
@@ -598,9 +598,18 @@
         else:
             errmsg = "Nested dtype involving objects should be supported."
             raise AssertionError(errmsg)
-        
 
 
+    def test_userconverters_with_explicit_dtype(self):
+        "Test user_converters w/ explicit (standard) dtype"
+        data = StringIO.StringIO('skip,skip,2001-01-01,1.0,skip')
+        test = np.genfromtxt(data, delimiter=",", names=None, dtype=float,
+                             usecols=(2, 3), converters={2: str})
+        control = np.array([('2001-01-01', 1.)],
+                           dtype=[('', '|S10'), ('', float)])
+        assert_equal(test, control)
+
+
     def test_spacedelimiter(self):
         "Test space delimiter"
         data = StringIO.StringIO("1  2  3  4   5\n6  7  8  9  10")



More information about the Numpy-svn mailing list