MCPcopy Index your code
hub / github.com/numpy/numpy / test_usecols

Method test_usecols

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

Source from the content-addressed store, hash-verified

864 assert_array_equal(x, a)
865
866 def test_usecols(self):
867 a = np.array([[1, 2], [3, 4]], float)
868 c = BytesIO()
869 np.savetxt(c, a)
870 c.seek(0)
871 x = np.loadtxt(c, dtype=float, usecols=(1,))
872 assert_array_equal(x, a[:, 1])
873
874 a = np.array([[1, 2, 3], [3, 4, 5]], float)
875 c = BytesIO()
876 np.savetxt(c, a)
877 c.seek(0)
878 x = np.loadtxt(c, dtype=float, usecols=(1, 2))
879 assert_array_equal(x, a[:, 1:])
880
881 # Testing with arrays instead of tuples.
882 c.seek(0)
883 x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2]))
884 assert_array_equal(x, a[:, 1:])
885
886 # Testing with an integer instead of a sequence
887 for int_type in [int, np.int8, np.int16,
888 np.int32, np.int64, np.uint8, np.uint16,
889 np.uint32, np.uint64]:
890 to_read = int_type(1)
891 c.seek(0)
892 x = np.loadtxt(c, dtype=float, usecols=to_read)
893 assert_array_equal(x, a[:, 1])
894
895 # Testing with some crazy custom integer type
896 class CrazyInt:
897 def __index__(self):
898 return 1
899
900 crazy_int = CrazyInt()
901 c.seek(0)
902 x = np.loadtxt(c, dtype=float, usecols=crazy_int)
903 assert_array_equal(x, a[:, 1])
904
905 c.seek(0)
906 x = np.loadtxt(c, dtype=float, usecols=(crazy_int,))
907 assert_array_equal(x, a[:, 1])
908
909 # Checking with dtypes defined converters.
910 data = '''JOE 70.1 25.3
911 BOB 60.5 27.9
912 '''
913 c = TextIO(data)
914 names = ['stid', 'temp']
915 dtypes = ['S4', 'f8']
916 arr = np.loadtxt(c, usecols=(0, 2), dtype=list(zip(names, dtypes)))
917 assert_equal(arr['stid'], [b"JOE", b"BOB"])
918 assert_equal(arr['temp'], [25.3, 27.9])
919
920 # Testing non-ints in usecols
921 c.seek(0)
922 bogus_idx = 1.5
923 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