summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2014-04-05 14:49:37 -0600
committerCharles Harris <charlesr.harris@gmail.com>2014-04-05 14:49:37 -0600
commit6857173fc1df9a5f196c9fab3249680c7694b493 (patch)
treeef28ee1f3d849e3c71f2fe8fd7c6f95ef86c8492 /numpy/lib
parenta0794f63d548e688e2eed76a9dc4e8df0ea33846 (diff)
parent079ca4d2da488ffb7af1fc923728c3abde867231 (diff)
downloadnumpy-6857173fc1df9a5f196c9fab3249680c7694b493.tar.gz
Merge pull request #4588 from ddasilva/ddasilva/2591
ENH: Better error w/ line num for bad column count in np.loadtxt()
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py5
-rw-r--r--numpy/lib/tests/test_io.py15
2 files changed, 18 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index f69ca0c73..98b4b6e35 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -845,6 +845,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
continue
if usecols:
vals = [vals[i] for i in usecols]
+ if len(vals) != N:
+ line_num = i + skiprows + 1
+ raise ValueError("Wrong number of columns at line %d"
+ % line_num)
+
# Convert each value according to its column and store
items = [conv(val) for (conv, val) in zip(converters, vals)]
# Then pack it according to the dtype's nesting
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 418386e35..d0f81bde3 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -19,10 +19,13 @@ from numpy.lib._iotools import (ConverterError, ConverterLockError,
ConversionWarning)
from numpy.compat import asbytes, asbytes_nested, bytes, asstr
from nose import SkipTest
-from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal,
- assert_raises, run_module_suite)
+from numpy.ma.testutils import (
+ TestCase, assert_equal, assert_array_equal,
+ assert_raises, assert_raises_regex, run_module_suite
+)
from numpy.testing import assert_warns, assert_, build_err_msg
+
@contextlib.contextmanager
def tempdir(change_dir=False):
tmpdir = mkdtemp()
@@ -759,6 +762,14 @@ class TestLoadTxt(TestCase):
res = np.loadtxt(count())
assert_array_equal(res, np.arange(10))
+ def test_bad_line(self):
+ c = TextIO()
+ c.write('1 2 3\n4 5 6\n2 3')
+ c.seek(0)
+
+ # Check for exception and that exception contains line number
+ assert_raises_regex(ValueError, "3", np.loadtxt, c)
+
class Testfromregex(TestCase):
# np.fromregex expects files opened in binary mode.