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

Method test_header_column_names

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

Source from the content-addressed store, hash-verified

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