diff options
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/io.py | 12 | ||||
-rw-r--r-- | numpy/lib/tests/test__iotools.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 13 |
3 files changed, 30 insertions, 3 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 4ff2a3945..12765e17c 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -837,6 +837,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skiprows=0, 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 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skiprows=0, 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 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skiprows=0, 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: diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index bb1483186..2cb8461c3 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -142,6 +142,14 @@ class TestStringConverter(TestCase): 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')) #------------------------------------------------------------------------------- diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 03d4f42e6..935773134 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -573,7 +573,7 @@ M 33 21.99 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,7 +598,16 @@ M 33 21.99 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): |