diff options
author | Alan Briolat <alan.briolat@gmail.com> | 2014-04-29 11:23:10 +0100 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-23 14:58:50 -0700 |
commit | 1b88f08b4f27e26adc7a1200a51974eb5bf719e0 (patch) | |
tree | 2c625fe5bdf78f59c3d6624050c619cd867fb281 /numpy/lib | |
parent | d44604ef625ff38e835a51cad3bcd24400278eff (diff) | |
download | numpy-1b88f08b4f27e26adc7a1200a51974eb5bf719e0.tar.gz |
BUG: Fix genfromtext NameValidator arguments passed to easy_dtype.
np.genfromtxt validates field names twice: once in genfromtxt and once
in easy_dtype. Whilst the arguments to genfromtxt are used in the first
validation, they aren't passed to easy_dtype (which is used only when
dtype != None) and therefore in this case the default validation (strip
non-alphanum, replace spaces) gets confusingly applied, ignoring
genfromtxt's arguments.
This patch adds fixes genfromtxt by passing the appropriate arguments
onwards to easy_dtype. That is probably the least invasive way to fix
the issue.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/npyio.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 24 |
2 files changed, 29 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index a40de4fea..04b57e7bb 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1451,7 +1451,11 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, names = validate_names(names) # Get the dtype if dtype is not None: - dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names) + dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names, + excludelist=excludelist, + deletechars=deletechars, + case_sensitive=case_sensitive, + replace_space=replace_space) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 81bddfadd..1dcffcd64 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1514,6 +1514,30 @@ M 33 21.99 ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype) assert_equal(test, ctrl) + def test_replace_space_known_dtype(self): + "Test the 'replace_space' (and related) options when dtype != None" + txt = "A.A, B (B), C:C\n1, 2, 3" + # Test default: replace ' ' by '_' and delete non-alphanum chars + test = np.genfromtxt(TextIO(txt), + delimiter=",", names=True, dtype=int) + ctrl_dtype = [("AA", int), ("B_B", int), ("CC", int)] + ctrl = np.array((1, 2, 3), dtype=ctrl_dtype) + assert_equal(test, ctrl) + # Test: no replace, no delete + test = np.genfromtxt(TextIO(txt), + delimiter=",", names=True, dtype=int, + replace_space='', deletechars='') + ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", int)] + ctrl = np.array((1, 2, 3), dtype=ctrl_dtype) + assert_equal(test, ctrl) + # Test: no delete (spaces are replaced by _) + test = np.genfromtxt(TextIO(txt), + delimiter=",", names=True, dtype=int, + deletechars='') + ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", int)] + ctrl = np.array((1, 2, 3), dtype=ctrl_dtype) + assert_equal(test, ctrl) + def test_incomplete_names(self): "Test w/ incomplete names" data = "A,,C\n0,1,2\n3,4,5" |