MCPcopy Create free account
hub / github.com/numpy/numpy / test_usecols

Method test_usecols

numpy/lib/tests/test_io.py:851–918  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

849 assert_array_equal(x, a)
850
851 def test_usecols(self):
852 a = np.array([[1, 2], [3, 4]], float)
853 c = BytesIO()
854 np.savetxt(c, a)
855 c.seek(0)
856 x = np.loadtxt(c, dtype=float, usecols=(1,))
857 assert_array_equal(x, a[:, 1])
858
859 a = np.array([[1, 2, 3], [3, 4, 5]], float)
860 c = BytesIO()
861 np.savetxt(c, a)
862 c.seek(0)
863 x = np.loadtxt(c, dtype=float, usecols=(1, 2))
864 assert_array_equal(x, a[:, 1:])
865
866 # Testing with arrays instead of tuples.
867 c.seek(0)
868 x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2]))
869 assert_array_equal(x, a[:, 1:])
870
871 # Testing with an integer instead of a sequence
872 for int_type in [int, np.int8, np.int16,
873 np.int32, np.int64, np.uint8, np.uint16,
874 np.uint32, np.uint64]:
875 to_read = int_type(1)
876 c.seek(0)
877 x = np.loadtxt(c, dtype=float, usecols=to_read)
878 assert_array_equal(x, a[:, 1])
879
880 # Testing with some crazy custom integer type
881 class CrazyInt:
882 def __index__(self):
883 return 1
884
885 crazy_int = CrazyInt()
886 c.seek(0)
887 x = np.loadtxt(c, dtype=float, usecols=crazy_int)
888 assert_array_equal(x, a[:, 1])
889
890 c.seek(0)
891 x = np.loadtxt(c, dtype=float, usecols=(crazy_int,))
892 assert_array_equal(x, a[:, 1])
893
894 # Checking with dtypes defined converters.
895 data = '''JOE 70.1 25.3
896 BOB 60.5 27.9
897 '''
898 c = TextIO(data)
899 names = ['stid', 'temp']
900 dtypes = ['S4', 'f8']
901 arr = np.loadtxt(c, usecols=(0, 2), dtype=list(zip(names, dtypes)))
902 assert_equal(arr['stid'], [b"JOE", b"BOB"])
903 assert_equal(arr['temp'], [25.3, 27.9])
904
905 # Testing non-ints in usecols
906 c.seek(0)
907 bogus_idx = 1.5
908 assert_raises_regex(

Callers

nothing calls this directly

Calls 6

assert_array_equalFunction · 0.90
assert_equalFunction · 0.90
assert_raises_regexFunction · 0.90
CrazyIntClass · 0.85
TextIOClass · 0.85
seekMethod · 0.80

Tested by

no test coverage detected