summaryrefslogtreecommitdiff
path: root/numpy/lib/tests
diff options
context:
space:
mode:
authorseberg <sebastian@sipsolutions.net>2016-02-01 14:15:19 +0100
committerseberg <sebastian@sipsolutions.net>2016-02-01 14:15:19 +0100
commitd1dada15f60c3de9aa35eb58cc4ed41c3ebf21c4 (patch)
tree5b7f841a814c9c5747855f19a18eadd2014b616d /numpy/lib/tests
parentea9775606c8303a3fd65fdf21a4a02846cef971e (diff)
parent849b81804fb7a11dc80821dbd166562225c8450f (diff)
downloadnumpy-d1dada15f60c3de9aa35eb58cc4ed41c3ebf21c4.tar.gz
Merge pull request #6656 from I--P/loadtxt-int
ENH: usecols now accepts an int when only one column has to be read
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r--numpy/lib/tests/test_io.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 226dc88fa..c0f8c1953 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -608,6 +608,29 @@ class TestLoadTxt(TestCase):
x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2]))
assert_array_equal(x, a[:, 1:])
+ # Testing with an integer instead of a sequence
+ for int_type in [int, np.int8, np.int16,
+ np.int32, np.int64, np.uint8, np.uint16,
+ np.uint32, np.uint64]:
+ to_read = int_type(1)
+ c.seek(0)
+ x = np.loadtxt(c, dtype=float, usecols=to_read)
+ assert_array_equal(x, a[:, 1])
+
+ # Testing with some crazy custom integer type
+ class CrazyInt(object):
+ def __index__(self):
+ return 1
+
+ crazy_int = CrazyInt()
+ c.seek(0)
+ x = np.loadtxt(c, dtype=float, usecols=crazy_int)
+ assert_array_equal(x, a[:, 1])
+
+ c.seek(0)
+ x = np.loadtxt(c, dtype=float, usecols=(crazy_int,))
+ assert_array_equal(x, a[:, 1])
+
# Checking with dtypes defined converters.
data = '''JOE 70.1 25.3
BOB 60.5 27.9
@@ -619,6 +642,21 @@ class TestLoadTxt(TestCase):
assert_equal(arr['stid'], [b"JOE", b"BOB"])
assert_equal(arr['temp'], [25.3, 27.9])
+ # Testing non-ints in usecols
+ c.seek(0)
+ bogus_idx = 1.5
+ assert_raises_regex(
+ TypeError,
+ '^usecols must be.*%s' % type(bogus_idx),
+ np.loadtxt, c, usecols=bogus_idx
+ )
+
+ assert_raises_regex(
+ TypeError,
+ '^usecols must be.*%s' % type(bogus_idx),
+ np.loadtxt, c, usecols=[0, bogus_idx, 0]
+ )
+
def test_fancy_dtype(self):
c = TextIO()
c.write('1,2,3.0\n4,5,6.0\n')