summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Cournapeau <cournape@gmail.com>2008-09-23 03:48:01 +0000
committerDavid Cournapeau <cournape@gmail.com>2008-09-23 03:48:01 +0000
commit96b1aa6b1f0ede5413693b21755eeb0ff57b102e (patch)
tree01c9b62115dd0d942986178fae87f05648992d1c
parent9536dc2b153b9204349c05dcd83da3cd72bc22af (diff)
parentf1855ef6c44ff037b606cbc0673a536ece31a945 (diff)
downloadnumpy-96b1aa6b1f0ede5413693b21755eeb0ff57b102e.tar.gz
Merged revisions 5844-5860 via svnmerge from
http://svn.scipy.org/svn/numpy/trunk ........ r5860 | stefan | 2008-09-23 05:28:00 +0900 (Tue, 23 Sep 2008) | 2 lines Ignore unused converters in `loadtxt`. ........
-rw-r--r--numpy/lib/io.py6
-rw-r--r--numpy/lib/tests/test_io.py11
2 files changed, 16 insertions, 1 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py
index 93c955942..caa790809 100644
--- a/numpy/lib/io.py
+++ b/numpy/lib/io.py
@@ -378,7 +378,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None,
# By preference, use the converters specified by the user
for i, conv in (user_converters or {}).iteritems():
if usecols:
- i = usecols.index(i)
+ try:
+ i = usecols.index(i)
+ except ValueError:
+ # Unused converter specified
+ continue
converters[i] = conv
# Parse each line, including the first
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 9cccbe041..d257b5423 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -258,6 +258,17 @@ class TestLoadTxt(TestCase):
c = StringIO.StringIO()
assert_raises(IOError, np.loadtxt, c)
+ def test_unused_converter(self):
+ c = StringIO.StringIO()
+ c.writelines(['1 21\n', '3 42\n'])
+ c.seek(0)
+ data = np.loadtxt(c, usecols=(1,), converters={0: lambda s: int(s, 16)})
+ assert_array_equal(data, [21, 42])
+
+ c.seek(0)
+ data = np.loadtxt(c, usecols=(1,), converters={1: lambda s: int(s, 16)})
+ assert_array_equal(data, [33, 66])
+
class Testfromregex(TestCase):
def test_record(self):
c = StringIO.StringIO()