8000 BUG: fix genfromtxt check of converters when using usecols · numpy/numpy@709a06d · GitHub
[go: up one dir, main page]

Skip to content

Commit 709a06d

Browse files
dhomeierjuliantaylor
authored andcommitted
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.
1 parent 79d3635 commit 709a06d

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

numpy/lib/npyio.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,22 +1574,25 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
15741574
for (miss, fill) in zipit]
15751575
# Update the converters to use the user-defined ones
15761576
uc_update = []
1577-
for (i, conv) in user_converters.items():
1577+
for (j, conv) in user_converters.items():
15781578
# If the converter is specified by column names, use the index instead
1579-
if _is_string_like(i):
1579+
if _is_string_like(j):
15801580
try:
1581-
i = names.index(i)
1581+
j = names.index(j)
1582+
i = j
15821583
except ValueError:
15831584
continue
15841585
elif usecols:
15851586
try:
1586-
i = usecols.index(i)
1587+
i = usecols.index(j)
15871588
except ValueError:
15881589
# Unused converter specified
15891590
continue
1590-
# Find the value to test:
1591+
else:
1592+
i = j
1593+
# Find the value to test - first_line is not filtered by usecols:
15911594
if len(first_line):
1592-
testing_value = first_values[i]
1595+
testing_value = first_values[j]
15931596
else:
15941597
testing_value = None
15951598
converters[i].update(conv, locked=True,

numpy/lib/tests/test_io.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,21 @@ def test_dtype_with_converters(self):
10931093
control = np.array([2009., 23., 46],)
10941094
assert_equal(test, control)
10951095

1096+
def test_dtype_with_converters_and_usecols(self):
1097+
dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n"
1098+
dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3}
1099+
dtyp = [('E1','i4'),('E2','i4'),('E3','i2'),('N', 'i1')]
1100+
conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
1101+
test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1102+
names=None, converters=conv)
1103+
control = np.rec.array([[1,5,-1,0], [2,8,-1,1], [3,3,-2,3]], dtype=dtyp)
1104+
assert_equal(test, control)
1105+
dtyp = [('E1','i4'),('E2','i4'),('N', 'i1')]
1106+
test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1107+
usecols=(0,1,3), n 5A9B ames=None, converters=conv)
1108+
control = np.rec.array([[1,5,0], [2,8,1], [3,3,3]], dtype=dtyp)
1109+
assert_equal(test, control)
1110+
10961111
def test_dtype_with_object(self):
10971112
"Test using an explicit dtype with an object"
10981113
from datetime import date

0 commit comments

Comments
 (0)
0