MCPcopy Create free account
hub / github.com/apache/arrow / test_header_column_names

Method test_header_column_names

python/pyarrow/tests/test_csv.py:853–902  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

851 }
852
853 def test_header_column_names(self):
854 rows = b"ab,cd\nef,gh\nij,kl\nmn,op\n"
855
856 opts = ReadOptions()
857 opts.column_names = ["x", "y"]
858 table = self.read_bytes(rows, read_options=opts)
859 self.check_names(table, ["x", "y"])
860 assert table.to_pydict() == {
861 "x": ["ab", "ef", "ij", "mn"],
862 "y": ["cd", "gh", "kl", "op"],
863 }
864
865 opts.skip_rows = 3
866 table = self.read_bytes(rows, read_options=opts)
867 self.check_names(table, ["x", "y"])
868 assert table.to_pydict() == {
869 "x": ["mn"],
870 "y": ["op"],
871 }
872
873 opts.skip_rows = 4
874 table = self.read_bytes(rows, read_options=opts)
875 self.check_names(table, ["x", "y"])
876 assert table.to_pydict() == {
877 "x": [],
878 "y": [],
879 }
880
881 opts.skip_rows = 5
882 with pytest.raises(pa.ArrowInvalid):
883 # Not enough rows
884 table = self.read_bytes(rows, read_options=opts)
885
886 # Unexpected number of columns
887 opts.skip_rows = 0
888 opts.column_names = ["x", "y", "z"]
889 with pytest.raises(pa.ArrowInvalid,
890 match="Expected 3 columns, got 2"):
891 table = self.read_bytes(rows, read_options=opts)
892
893 # Can skip rows with a different number of columns
894 rows = b"abcd\n,,,,,\nij,kl\nmn,op\n"
895 opts.skip_rows = 2
896 opts.column_names = ["x", "y"]
897 table = self.read_bytes(rows, read_options=opts)
898 self.check_names(table, ["x", "y"])
899 assert table.to_pydict() == {
900 "x": ["ij", "mn"],
901 "y": ["kl", "op"],
902 }
903
904 def test_header_autogenerate_column_names(self):
905 rows = b"ab,cd\nef,gh\nij,kl\nmn,op\n"

Callers

nothing calls this directly

Calls 3

read_bytesMethod · 0.95
ReadOptionsClass · 0.90
check_namesMethod · 0.45

Tested by

no test coverage detected