summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorDerek Homeier <dhomeie@gwdg.de>2014-08-27 13:42:32 +0200
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-08-27 15:50:28 +0200
commit709a06d0e9db862a8dd519db13724a4c59de7d69 (patch)
treefd1c844aa70fc050716347e61d717e055c6487ce /numpy/lib
parent79d36359bf18a122577054072d5c846aded3f2b4 (diff)
downloadnumpy-709a06d0e9db862a8dd519db13724a4c59de7d69.tar.gz
BUG: fix genfromtxt check of converters when using usecols
fixes an issue reported by Adrian Altenhoff where user-supplied converters in genfromtxt were not tested with the right first_values when also specifying usecols.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py15
-rw-r--r--numpy/lib/tests/test_io.py15
2 files changed, 24 insertions, 6 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index fe855a71a..e6b4d426f 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1574,22 +1574,25 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
for (miss, fill) in zipit]
# Update the converters to use the user-defined ones
uc_update = []
- for (i, conv) in user_converters.items():
+ for (j, conv) in user_converters.items():
# If the converter is specified by column names, use the index instead
- if _is_string_like(i):
+ if _is_string_like(j):
try:
- i = names.index(i)
+ j = names.index(j)
+ i = j
except ValueError:
continue
elif usecols:
try:
- i = usecols.index(i)
+ i = usecols.index(j)
except ValueError:
# Unused converter specified
continue
- # Find the value to test:
+ else:
+ i = j
+ # Find the value to test - first_line is not filtered by usecols:
if len(first_line):
- testing_value = first_values[i]
+ testing_value = first_values[j]
else:
testing_value = None
converters[i].update(conv, locked=True,
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 49ad1ba5b..3d02c35ca 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -1093,6 +1093,21 @@ M 33 21.99
control = np.array([2009., 23., 46],)
assert_equal(test, control)
+ def test_dtype_with_converters_and_usecols(self):
+ dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n"
+ dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3}
+ dtyp = [('E1','i4'),('E2','i4'),('E3','i2'),('N', 'i1')]
+ conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
+ test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
+ names=None, converters=conv)
+ control = np.rec.array([[1,5,-1,0], [2,8,-1,1], [3,3,-2,3]], dtype=dtyp)
+ assert_equal(test, control)
+ dtyp = [('E1','i4'),('E2','i4'),('N', 'i1')]
+ test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
+ usecols=(0,1,3), names=None, converters=conv)
+ control = np.rec.array([[1,5,0], [2,8,1], [3,3,3]], dtype=dtyp)
+ assert_equal(test, control)
+
def test_dtype_with_object(self):
"Test using an explicit dtype with an object"
from datetime import date