summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpierregm <pierregmcode@gmail.com>2010-10-13 12:06:23 +0200
committerpierregm <pierregmcode@gmail.com>2010-11-13 21:19:50 +0100
commita9d936ee92b1732256e30db44377b53769e6241e (patch)
tree1a626328c81cb763e59da122d7aa48256f54e230
parent4a7de57448091ef02e50edf9d1e302c20a26ff0c (diff)
downloadnumpy-a9d936ee92b1732256e30db44377b53769e6241e.tar.gz
Fixing genfromtxt: names w/ usecols (bug #1636)
-rw-r--r--numpy/lib/npyio.py5
-rw-r--r--numpy/lib/tests/test_io.py21
2 files changed, 24 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 2668357bc..8221f5c09 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1220,7 +1220,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
if fval in comments:
del first_values[0]
- # Check the columns to use
+ # Check the columns to use: make sure `usecols` is a list
if usecols is not None:
try:
usecols = [_.strip() for _ in usecols.split(",")]
@@ -1243,7 +1243,6 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
# Get the dtype
if dtype is not None:
dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names)
- names = dtype.names
# Make sure the names is a list (for 2.5)
if names is not None:
names = list(names)
@@ -1264,6 +1263,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
# If `names` is not None, update the names
elif (names is not None) and (len(names) > nbcols):
names = [names[_] for _ in usecols]
+ elif (names is not None) and (dtype is not None):
+ names = dtype.names
# Process the missing values ...............................
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index c1a19f5a4..1f4ed0a01 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -1133,6 +1133,27 @@ M 33 21.99
dtype=[('a', int), ('f0', float), ('f1', int)])
assert_equal(test, ctrl)
+ def test_names_with_usecols_bug1636(self):
+ "Make sure we pick up the right names w/ usecols"
+ data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4"
+ ctrl_names = ("A", "C", "E")
+ test = np.genfromtxt(StringIO(data),
+ dtype=(int, int, int), delimiter=",",
+ usecols=(0, 2, 4), names=True)
+ assert_equal(test.dtype.names, ctrl_names)
+ #
+ test = np.genfromtxt(StringIO(data),
+ dtype=(int, int, int), delimiter=",",
+ usecols=("A", "C", "E"), names=True)
+ assert_equal(test.dtype.names, ctrl_names)
+ #
+ test = np.genfromtxt(StringIO(data),
+ dtype=int, delimiter=",",
+ usecols=("A", "C", "E"), names=True)
+ assert_equal(test.dtype.names, ctrl_names)
+
+
+
def test_fixed_width_names(self):
"Test fix-width w/ names"
data = " A B C\n 0 1 2.3\n 45 67 9."