diff options
author | dhuard <dhuard@localhost> | 2008-07-22 00:49:31 +0000 |
---|---|---|
committer | dhuard <dhuard@localhost> | 2008-07-22 00:49:31 +0000 |
commit | e41b0e3b2222095c2eb75952602fc8b779798cbc (patch) | |
tree | 4a54e4259a0bf873d07ffb9eb6e1dc01d3bd39ae /numpy/lib | |
parent | 75779e8651774e1f86fd6656d851d22411e2224b (diff) | |
download | numpy-e41b0e3b2222095c2eb75952602fc8b779798cbc.tar.gz |
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.
Diffstat (limited to 'numpy/lib')
-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): |