diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/io.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 11 |
2 files changed, 17 insertions, 2 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 36723f1d8..b1ae192ec 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -292,8 +292,12 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, 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 diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index e083a915b..6ece0222f 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -220,6 +220,17 @@ class TestLoadTxt(TestCase): 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): |